C++17 filesystem

设置C++17

用VC的话, 默认是不开启C++的, 设置一下

image

使用

#include <filesystem>
using namespace std;
using fs = std::filesystem;

path类 创建一个path类

//下面大部分操作中的 文件名 实际上是个paeh类, 当然写成字符串也会转型

directory_entry类: 文件入口
directory_iterator类: 这是一个容器
file_status类: 获取和修改文件/目录的属性

操作

定位current_path()

cout<<"current path: "<<fs::current_path()<<std::endl;

增 使用fstream

ofstream ofs("example.txt");
ofs.close();

remove()

exists()

if(fs::exists(文件名))
{
    fs::remove(文件名);
    std::cout<<"file deleted"<<std::endl;
}

remame()

try
{
    fs::remame(旧文件名,新文件名);
}
catch(fs::filesystem_error& e)
{
    cout<<"file name change failed: "<<e.what()<<endl;
}

posted @ 2023-06-14 22:09  无形深空  阅读(65)  评论(0编辑  收藏  举报