std::filesystem使用笔记

C++17引入了filesystem标准库,该库提供了一组类和函数,用于处理文件系统中的文件和目录。使用库,可以方便地执行文件和目录的各种操作,如创建、复制、移动、删除文件或目录,遍历目录,获取文件属性等。

1. 头文件

cpp 复制代码
#include <filesystem>

2. 函数介绍

2.1. 路径判断

cpp 复制代码
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. 路径获取

cpp 复制代码
std::filesystem::initial_path();   // 得到程序运行时的系统当前路径
std::filesystem::current_path();   // 得到系统当前路径
std::filesystem::current_path(const Path& p);   // 改变当前路径
 
std::filesystem::path parentPath = p3.parent_path(); //获得父路径
std::filesystem::path parentPath = p3.system_complete(); //获得全路径(绝对路径)

2.3. 删除复制移动

cpp 复制代码
std::filesystem::remove(const Path& p, system::error_code & ec = singular);   // 删除文件
std::filesystem::remove_all(const Path& p);   // 递归删除文件夹中所有内容,返回删除文件的数量,类似于rm -rf p
std::filesystem::rename(const Path1& from_p, const Path2& to_p);   // 重命名,亦可实现移动文件(夹)的功能
std::filesystem::copy_file(const Path1& from_fp, const Path2& to_fp);   // 拷贝文件

2.4. 创建路径

cpp 复制代码
std::filesystem::create_directories(path) // 创建文件夹路径
std::filesystem::create_hard_link(const Path1& to_p, const Path2& from_p);  // 建立硬链接
std::filesystem::complete(const Path& p, const Path& base = initial_path<Path>());   // 以base以基,p作为相对路径,返回其完整路径
std::filesystem::create_directories(const Path & p);   // 建立路径
std::filesystem::error_code std::filesystem::create_hard_link(const Path1& to_p, const Path2& from_p, error_code& ec);   // 建立硬链接
std::filesystem::create_symlink(const Path1& to_p, const Path2& from_p);  // 建立软链接
std::filesystem::create_symlink(const Path1& to_p, const Path2& from_p, error_code& ec);   // 建立软链接
bool std::filesystem::create_directory(const Path& dp);   // 创建目录

2.5. 获取信息

cpp 复制代码
std::filesystem::space_info std::filesystem::space(const Path& p);   // 得到指定路径下的空间信息,space_info 有capacity, free 和 available三个成员变量,分别表示容量,剩余空间和可用空间。
std::filesystem::last_write_time(const Path& p);   // 最后修改时间
std::filesystem::last_write_time(const Path& p, const std::time_t new_time);   // 修改最后修改时间

2.6. 路径简化

有时路径写的过于冗长,使用canonical函数可以进行简化

cpp 复制代码
const std::filesystem::path file_path("/home/myname/Downloads/../../");
 
std::cout << file_path << std::endl;
std::cout << std::filesystem::canonical(file_path) << std::endl;

输出

/home/myname/Downloads/../..

/home

3. 组合功能

3.1. 遍历文件夹

3.1.1. 遍历所有子孙目录

cpp 复制代码
for (std::filesystem::directory_entry& file : std::filesystem::directory_iterator(path)) {
  if (std::filesystem::is_regular_file(file.status())) {
    std::string file_name = file.path().filename().string();
  }
}

3.1.2. 只遍历子目录

cpp 复制代码
std::filesystem::path dirpath;
std::filesystem::directory_iterator end;// 只支持本层目录遍历
for (std::filesystem::directory_iterator iter(dirpath); iter != end; iter++)
{
  std::cout << iter->leaf().string() << std::endl; // 叶结点文件名
  std::cout << iter->path().stem().string() << std::endl; // 叶结点文件名(无后缀)
  std::cout << iter->path().filename().string() << std::endl; // 文件名(不带引号)
  std::cout << iter->path().string() << std::endl; // 叶子结点路径
}

3.2. 获取相对路径

cpp 复制代码
std::filesystem::path p1("/user/dir");
p1.relative_path(); //获得path的相对路径

3.3. 拼接绝对路径

cpp 复制代码
// 目录路径
std::filesystem::path dir_path("path/to/directory");
 
// 文件名或相对路径
std::string filename = "example.txt";
 
// 创建完整路径
std::filesystem::path full_path = dir_path / filename;

参考文献

c++中filesystem的用法是什么 - 问答 - 亿速云

boost::filesystem常用功能_is_regular_file-CSDN博客

相关推荐
无夜_10 分钟前
Prototype(原型模式)
开发语言·c++
刘好念29 分钟前
[图形学]smallpt代码详解(1)
c++·计算机图形学
fpcc34 分钟前
并行编程实战——TBB框架的应用之一Supra的基础
c++·并行编程
兵哥工控34 分钟前
MFC工控项目实例二十二主界面计数背景颜色改变
c++·mfc
兵哥工控37 分钟前
MFC工控项目实例二十手动测试界面模拟量输入实时显示
c++·mfc
jyan_敬言1 小时前
【Linux】Linux命令与操作详解(一)文件管理(文件命令)、用户与用户组管理(创建、删除用户/组)
linux·运维·服务器·c语言·开发语言·汇编·c++
笑非不退1 小时前
C++ 异步编程 并发编程技术
开发语言·c++
T0uken2 小时前
【QT Qucik】C++交互:接收QML信号
c++·qt·交互
爱写代码的刚子2 小时前
C++知识总结
java·开发语言·c++
s_little_monster2 小时前
【QT】QT入门
数据库·c++·经验分享·笔记·qt·学习·mfc