如何使用文件系统C ++库创建文件?
我知道创建文件有不同的方法,但我对文件系统库感兴趣。
答案 0 :(得分:1)
此代码将创建一个文件夹和一个txt文件(+在其中添加一些文本)
#include <iostream>
#include <filesystem>
#include <fstream>
int main()
{
std::filesystem::path path{ "C:\\TestingFolder" }; //creates TestingFolder object on C:
path /= "my new file.txt"; //put something into there
std::filesystem::create_directories(path.parent_path()); //add directories based on the object path (without this line it will not work)
std::ofstream ofs(path);
ofs << "this is some text in the new file\n";
ofs.close();
return 0;
}