• C++17 std::filesystem 用法指北


    写在最前:std::filesystem 需要C++17或以上的支持,如果你也是使用visual studio开发,那么可以通过 项目属性 > 配置属性 > C/C++ > 语言 > C++语言标准 进行设置。

    本文将针对常用的场景,对std::filesystem的使用逐一进行验证:

    • <1> 判断文件夹是否存在
    • <2> 创建单层目录
    • <3> 逐级创建多层目录
    • <4> 创建多级目录
    • <5> 当前文件路径
    • <6> 创建文件"from.dat"
    • <7> 获取相对于base的绝对路径
    • <8> 文件拷贝
    • <9> 移动文件或重命名
    • <10> 创建文件 “example.dat”
    • <11> 获取文件大小
    • <12> 获取文件最后修改时间
    • <13> 删除文件
    • <14> 递归删除目录下所有文件
    • <15> 在临时文件夹下创建文件夹并删除

    示例代码

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    namespace fs = std::filesystem;
    
    int main() 
    {
    	cout << "fs demo:\n" << std::boolalpha;
    
    	//<1> 判断文件夹是否存在
    	string dirName("log");
    	fs::path url(dirName);
    	if (!fs::exists(url)) {
    		cout << std::quoted(dirName) << " not exist" << "\n";
    	}
    	else {
    		cout << std::quoted(dirName) << " not exist" << "\n";
    	}
    
    	//<2> 创建单层目录
    	bool okey = fs::create_directory(dirName);
    	cout << "create_directory(" << std::quoted(dirName) << "), result=" << okey << "\n";
    
    	//<3> 逐级创建多层目录
    	error_code err;
    	string subDir = dirName + "/subdir";
    	okey = fs::create_directory(subDir, err);
    	cout << "create_directory(" << std::quoted(subDir) << "), result=" << okey << "\n";
    	cout << "err.value()=" << err.value() << " err.message()=" << err.message() << "\n";
    
    	//<4> 创建多级目录
    	dirName = "a/b//c/d/";
    	okey = fs::create_directories(dirName, err);
    	cout << "create_directories(" << std::quoted(dirName) << "), result=" << okey << "\n";
    	cout << "create_directories, err.value()=" << err.value() << " err.message()=" << err.message() << "\n";
    
    	//<5> 当前文件路径
    	fs::path currentPath = fs::current_path();//C:\Users\Kandy\Desktop\fs\fs
    	cout << "currentPath:\t" << currentPath << "\n";//当前路径
    	cout << "root_directory:\t" << currentPath.root_directory() << "\n";//根目录
    	cout << "relative_path:\t" << currentPath.relative_path() << "\n";//相对路径
    	cout << "root_name:\t" << currentPath.root_name() << "\n";//根名
    	cout << "root_path:\t" << currentPath.root_path() << "\n";//根路径
    
    	//<6> 创建文件"from.dat"
    	fs::path oldPath(fs::current_path() / "from.dat");
    	fstream file(oldPath, ios::out | ios::trunc);
    	if (!file) {
    		cout << "create file(" << oldPath.string() << ") failed!" << "\n";
    	}
    	file.close();
    
    	//<7> 获取相对于base的绝对路径
    	fs::path absPath = fs::absolute(oldPath/*, fs::current_path()*/);
    	cout << "absPath=" << absPath.string() <<"\n";//"C:\Users\Kandy\Desktop\filesystem\filesystem\from.dat"
    
    	//<8> 文件拷贝
    	fs::create_directories(fs::current_path() / "to");
    	fs::path toPath(fs::current_path() / "to/from0.dat");
    	fs::copy(oldPath, toPath);
    
    	//<9> 移动文件或重命名
    	fs::path newPath(fs::current_path() / "to/to.dat");
    	fs::rename(oldPath, newPath);
    
    	//<10> 创建文件 "example.dat"
    	fs::path _path = fs::current_path() / "example.dat";
    	cout << "example.dat path:" << std::quoted(_path.string()) << "\n";
    	std::ofstream(_path).put('a'); // create file of size 1
    	std::ofstream(_path).close();
    
    	//文件类型判定
    	assert(fs::file_type::regular == fs::status(_path).type());
    
    	//<11> 获取文件大小
    	auto size = fs::file_size(_path);
    	cout << "file_size=" << size << endl;
    
    	//<12> 获取文件最后修改时间
    	auto time = fs::last_write_time(_path);
    	cout << "last_write_time=" << time.time_since_epoch().count() << endl;
    
    	//<13> 删除文件
    	okey = fs::remove(_path);
    	cout << "remove(" << std::quoted(_path.string()) << ")" << okey << endl;
    
    	//<14> 递归删除目录下所有文件,返回被成功删除的文件个数
    	uintmax_t count = fs::remove_all(dirName);//dirName="a/b//c/d/",会把d目录也删掉
    	cout << "remove_all(" << std::quoted(dirName) << ")" << count << endl;
    
    	//<15> 在临时文件夹下创建文件夹并删除
    	fs::path tmp = fs::temp_directory_path();//"C:\Users\Kandy\AppData\Local\Temp\"
    	cout << "temp_directory_path=" << std::quoted(tmp.string()) << endl;
    	fs::create_directories(tmp / "_abcdef/example");
    	std::uintmax_t n = fs::remove_all(tmp / "_abcdef");
    	std::cout << "Deleted " << n << " files or directories\n";
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    C++官方文档

    链接地址:https://en.cppreference.com/w/cpp/filesystem

  • 相关阅读:
    QML 3D入门知识路线
    微信小程序 typescript 开发日历界面
    【高效办公】一、ubuntu之间共享桌面
    极客时间Kafka - 05 Kafka 生产者发送消息可靠性保障|幂等生产者和事务生产者
    成为 Java 顶尖程序员之前,先过了下面问题才行
    夜莺n9ev5配置pushgateway
    立方体IV(暑假每日一题 10)
    对基本数据类型、String类型、Object类型元素所构成的数组或List集合进行排序
    leetcode698. 划分为k个相等的子集
    【Linux网络编程】基础API
  • 原文地址:https://blog.csdn.net/hellokandy/article/details/126381514