我希望下面的行使用数组中给定的内容写入一个新文件
但进入名为 logs
的新文件夹:
char log_file_name[100]; /* this array contains the name of a new file */
ofstream write_to_log;
write_to_log.open (relative path, log_file_name , fstream::app);
如何让它工作?
最佳答案
您可以使用 CreateDirectory
在 Windows 中使用 VC++ 创建文件夹。
#include <windows.h>
#include <string>
#include <fstream>
using namespace std;
int main() {
string path = "C:\\users\\folder";
CreateDirectory(path.c_str(), NULL);
char log_file_name[100] = "log.txt";
path += '\\';
path += log_file_name;
ofstream write_to_log(path.c_str(), fstream::app);
return 0;
}
关于c++ ofstream write_to_log.open ("relative path + array");,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424528/