C++17引入了filesystem标准库,该库提供了一组类和函数,用于处理文件系统中的文件和目录。使用库,可以方便地执行文件和目录的各种操作,如创建、复制、移动、删除文件或目录,遍历目录,获取文件属性等。
1. 头文件
#include <filesystem>
2. 函数介绍
2.1. 路径判断
std::filesystem::system_complete(path); // 返回完整路径(相对路径 + 当前路径)
std::filesystem::exists(path); // 目录是否存在
std::filesystem::is_directory(path); // 是否是路径
std::filesystem::is_empty(path); // 文件夹是否为空,必须保证路径存在,否则抛异常
std::filesystem::is_regular_file(path); // 是否是普通文件
std::filesystem::is_symlink(path); // 是否是一个链接文件
std::filesystem::file_status std::filesystem::status(path); // 返回路径名对应的状态
2.2. 路径获取
std::filesystem::initial_path(); // 得到程序运行时的系统当前路径
std::filesystem::current_path(); // 得到系统当前路径
std::filesystem::current_path(const Path& p); // 改变当前路径
std::filesystem::path