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博客

相关推荐
爱学习的小邓同学3 小时前
C++ --- 多态
开发语言·c++
招摇的一半月亮9 小时前
P2242 公路维修问题
数据结构·c++·算法
f***019310 小时前
CC++链接数据库(MySQL)超级详细指南
c语言·数据库·c++
合方圆~小文10 小时前
球型摄像机作为现代监控系统的核心设备
java·数据库·c++·人工智能
椰萝Yerosius11 小时前
[题解]2024CCPC郑州站——Z-order Curve
c++·算法
滨HI014 小时前
C++ opencv简化轮廓
开发语言·c++·opencv
学习路上_write15 小时前
FREERTOS_互斥量_创建和使用
c语言·开发语言·c++·stm32·单片机·嵌入式硬件
闻缺陷则喜何志丹16 小时前
【SOSDP模板 容斥原理 逆向思考】3757. 有效子序列的数量|分数未知
c++·算法·力扣·容斥原理·sosdp·逆向思考
BestOrNothing_201516 小时前
一篇搞懂 C++ 重载:函数重载 + 运算符重载,从入门到会用(含 ++、<<、== 实战)
c++·函数重载·运算符重载·operator·前置后置++·重载与重写
2501_9411444217 小时前
Python + C++ 异构微服务设计与优化
c++·python·微服务