写在最前: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 <iostream>
#include <filesystem>
#include <fstream>
#include <string>
#include <set>
#include <assert.h>
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;
}