开发者

Output file to specific folder C++ Windows 7

I am using C++ and trying to output a file to a specific place, a fol开发者_高级运维der with a specified name in the same directory as the executable. Couldn't find a great resource on an easy way to do this but I know it must be possible.

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

Thank you for your time!

Edit: I used mkdir to create a folder but how do I output to that folder. Is mkdir even a good thing to be using? I want to learn the best way to do this, not necessarily the easiest.


This code:

#include <fstream>
#include <iostream>

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

will write "hello" to the file log.txt in the directory c:\mydir\somewhere, assuming the directory exists. And yes, mkdir is the right function to use. If you don't want to hardcode the path, you can find the path & name of the executable with GetModuleFileName, and then create the path programatically from that - see How to get Current Directory? for an example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜