Output file to specific folder C++ Windows 7

大憨熊 提交于 2019-12-23 12:43:08

问题


I am using C++ and trying to output a file to a specific place, a folder 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.


回答1:


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.



来源:https://stackoverflow.com/questions/5868246/output-file-to-specific-folder-c-windows-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!