| ToniAz (120) | |||||||
|
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:
but didn't work. Someone suggested in some forum to use direct.h as follows:
but I think it's kind of slow. Any suggestions? Side question, the following code:
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
|
|||||||
| MorganAW (3) | |||
Try this....
| |||
|
Last edited on
|
|||
| Stewbond (1135) | |||
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:
| |||
|
Last edited on
|
|||
| andywestken (1477) | |
|
@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
|
|
| ToniAz (120) | |
|
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
|
|
| Stewbond (1135) | |
|
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. | |
|
|
|