The output file to a specific folder C ++ Windows 7

I use C ++ and try to output the file to a specific location, a folder with the specified name in the same directory as the executable. It is not possible to find a great resource for an easy way to do this, but I know that it should be possible.

My example. I save the log file and instead of saving it in the same directory as the executable file, it saves / logs /

Thank you for your time!

Edit: I used mkdir to create the folder, but how can I output it to this folder. Is mkdir a good product? I want to know the best way to do this, not necessarily the easiest.

+4
source share
1 answer

This code:

#include <fstream> #include <iostream> int main() { std::ofstream of( "C:\\mydir\\somewhere\\log.txt" ); of << "hello\n"; } 

will write hello to the log.txt file in the c: \ mydir \ directory somewhere, assuming the directory exists. And yes, mkdir is the right function to use. If you do not want to specify the path hard, you can find the path and name of the executable file with GetModuleFileName , and then create the path programmatically from this - see How to get the current directory? for example.

+6
source

All Articles