且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-29 14:47:28

此代码将创建一个文件夹和一个txt文件(+在其中添加一些文本)

This code will create a folder and a txt file (+ adds some text into there)

#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;
}