Outputting to working directroy

Hello everyone!

I'mtrying to use ofstream to output to a file in the working directory. The file doesn't exist and neither does the directory.

I was trying something like:

1
2
3
4
5
    
    ofstream out;
    out.open("folder\\file");
    out << "here" << endl;
    out.close();


but didn't work. Someone suggested in some forum to use direct.h as follows:

1
2
3
4
5
    mkdir("folder");
    ofstream out;
    out.open("folder\\file");
    out << "here" << endl;
    out.close();


but I think it's kind of slow. Any suggestions?

Side question, the following code:

1
2
3
4
5
6
7
8
9
    
    ofstream out;
    out.open("folder/file");
    if (out.is_open())
    {
        cout << "open" << endl;
    }
    out << "here" << endl;
    out.close();


How could this be?! There was no file written to disk, so file cannot be open, but is_open() returned true!

Thanks!
Last edited on
Try this....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<fstream>

int main(void)
{

char filetext;
filetext = "The text inside file goes here, or replace with string variable";

ofstream outfile("folder\\file.ext");
outfile.put(filetext);
outfile.close();

system("PAUSE");

return 0;

}
Last edited on
To open a file in the current directory use:

ofstream outfile("file.ext")
or
ofstream outfile(".\\file.ext")

To open a file in the directory above the current one use:
ofstream outfile("..\\file.ext")

To open a file two directories up, use:
ofstream outfile("..\\..\\file.ext")

To open a file in a directory below the current one use:
ofstream outfile(".\\folder\\file.ext")

etc...

To ensure that the directory is created:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <windows.h>

int main()
{
	WCHAR* folder = L".\\TestFolder";
	WCHAR* file = L"File.ext";

	std::wstring path = folder;
	path += L"\\";
	path += file;

	std::ofstream fout(path);

	if(fout.fail()) // if fout failed to open the file, then the folder probably isn't there
	{
		CreateDirectory(folder, NULL); // From windows.h, creates a folder
		fout.open(path);
	}

	fout << "Hello";
	return 0;
}
Last edited on
@ToniAz You have to use either mkdir or (on Windows) CreateDirectory, as ofstream does not know how to create a directory (you did say tht the directory did not exist?).

Note that neither mkdir() nor CreateDirectory() can create more that a single level at a time.

And "folder\\file" and ".\\folder\\file" identify the same file path. As does "./folder/file". Windows happily accepts Linux style paths as inputs, but if you retrieve a path from Windows, it will be in Windows format.

Andy
Last edited on
Thank you all for your help

@andywestken

Yeah that whole mixing up between forward slash and backlash always gets me!

@Stewbond

Since you mentioned the the wide string support, why did you interchangingly used wstring and WCHAR*. Why not use wstring ony?
Also, is wstring supported by windows.h? Do you recommend using wstring and not regular string in web-based programming?

Thanks again!
Last edited on
Whoop, I haven't checked this thread for a while.

wtring is partially included in #include <iostream> and supported in full with #include <string> .

Why do I use wstring over string and WCHAR* over CHAR*? Because the windows function CreateDirectory() only accepts wide characters. These are 16-bit characters instead of 8-bit characters.

Topic archived. No new replies allowed.