使用文件系统 C 库创建文件

新手上路,请多包涵

如何使用文件系统 C++ 库创建文件?

我知道创建文件有不同的方法,但我对文件系统库特别感兴趣。

原文由 Vallabh Patade 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 382
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;
}

原文由 Nirvikalpa Samadhi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题