使用文件系统C ++库创建文件 [英] Create file with filesystem C++ library

查看:76
本文介绍了使用文件系统C ++库创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to create a file with filesystem C++ library?

我知道创建文件的方法有很多,但是我对文件系统库很感兴趣.

I know there are different ways to create a file but I am perticularily intrested with filesystem library.

推荐答案

此代码将创建一个文件夹和一个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;
}

这篇关于使用文件系统C ++库创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆