C++17 文件系统库核心功能解析
文件系统库
官方文档:https://en.cppreference.com/w/cpp/filesystem
该库源自boost.filesystem,自C++17起被纳入ISO标准,用于管理文件系统元素(路径、文件、目录等)。若需在旧版C++编译器中使用,需改用boost库。
1、核心类
- 头文件
<filesystem> - 命名空间
std::filesystem
以下类名可通过官方文档链接直接查看详细说明:
| path | 路径对象 |
| fs_error | 文件系统异常 |
| dir_entry | 目录条目 |
| dir_iterator | 目录迭代器 |
| rec_dir_iterator | 递归遍历目录 |
| file_status | 文件元信息 |
| space_info | 存储空间数据 |
| file_type | 文件类型标识 |
| perms | 权限设置 |
| perm_opt | 权限操作选项 |
| copy_opt | 复制操作参数 |
| dir_opt | 目录遍历选项 |
| file_time | 时间戳信息 |
2、关键函数
- 头文件
<filesystem> - 命名空间
std::filesystem
| absolute | 转换绝对路径 |
| canonical | 规范化路径 |
| relative | 计算相对路径 |
| copy | 文件/目录复制 |
| copy_file | 文件内容复制 |
| copy_link | 链接复制 |
| make_dir | 创建目录 |
| make_hard_link | 创建硬链接 |
| make_symlink | 创建符号链接 |
| curr_path | 获取/设置当前路径 |
| exists | 检查路径有效性 |
| equal | 比较路径一致性 |
| file_size | 获取文件大小 |
| link_count | 获取链接数 |
| mod_time | 获取修改时间 |
| set_perms | 修改权限 |
| read_link | 获取链接目标 |
| del | 删除文件/目录 |
| rename | 重命名文件 |
| resize | 调整文件大小 |
| disk_space | 获取磁盘信息 |
| meta | 获取元数据 |
| temp_path | 临时目录路径 |
2.1、绝对路径转换
函数原型:
path abs_path(const path& p);
path abs_path(const path& p, error_code& ec);
异常处理:
- 内存不足时抛出bad_alloc
- 系统调用失败时设置错误码
说明:
- 对POSIX系统等同于当前路径拼接
- Windows使用GetFullPathNameW实现
示例:
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main() {
fs::path p = "test.txt";
std::cout << "Current: " << fs::current_path() << '\n';
std::cout << "Abs: " << fs::absolute(p) << '\n';
}
2.2、路径规范化
函数原型:
path norm_path(const path& p);
path norm_path(const path& p, error_code& ec);
path weak_norm(const path& p);
path weak_norm(const path& p, error_code& ec);
差异说明:
- canonical严格验证路径有效性
- weakly_canonical允许部分路径不存在
示例:
// 创建测试目录结构
auto tmp = fs::temp_directory_path();
auto d1 = tmp / "a/b/c1/d";
auto d2 = tmp / "a/b/c2/e";
fs::create_directories(d1);
fs::create_directories(d2);
fs::current_path(d1);
auto p = "../../c2/./e";
std::cout << "Norm: " << fs::canonical(p) << '\n';
std::cout << "Weak: " << fs::weakly_canonical("../nonexist") << '\n';
2.3、相对路径计算
函数原型:
path rel_path(const path& p, const path& base);
path prox_path(const path& p, const path& base);
示例:
show("/a/b/c", "/a/b"); // 输出"c"
show("/a/c", "/a/b"); // 输出"../c"
show("c", "/a/b"); // 输出空
2.4、复制操作
函数原型:
void copy(const path& src, const path& dst, copy_options opts);
选项说明:
- update_existing 更新已有文件
- recursive 递归复制
- directories_only 仅复制目录
示例:
fs::copy("source_dir", "dest_dir",
fs::copy_options::recursive | fs::copy_options::update_existing);
2.5、文件复制
函数原型:
bool copy_file(const path& src, const path& dst, copy_options opts);
行为规则:
- 目标存在时根据选项决定是否覆盖
- 支持硬链接/符号链接处理
示例:
fs::copy_file("input.txt", "output.txt",
fs::copy_options::overwrite_existing);
2.6、符号链接处理
函数原型:
void create_link(const path& target, const path& link);
注意事项:
- 不同系统对链接支持有差异
- 硬链接需文件存在
- 符号链接可跨文件系统
示例:
fs::create_symlink("target.txt", "link.txt");
2.7、目录操作
函数原型:
bool create_dir(const path& p);
bool create_dirs(const path& p);
特性说明:
- create_dirs递归创建多级目录
- 可指定父目录属性继承
示例:
fs::create_dirs("a/b/c");
2.8、权限管理
函数原型:
void set_perms(const path& p, perms opts);
选项说明:
- replace 替换权限
- add 添加权限
- remove 移除权限
示例:
fs::permissions("file.txt",
fs::perms::owner_all | fs::perms::group_read,
fs::perm_options::add);
2.9、路径操作
函数原型:
path get_current();
void set_current(const path& p);
示例:
std::cout << "Current: " << fs::current_path() << '\n';
fs::current_path("/new/path");
2.10、文件状态检测
函数原型:
bool exists(const path& p);
file_status status(const path& p);
示例:
if (fs::exists("file.txt")) {
auto st = fs::status("file.txt");
if (fs::is_regular_file(st)) {
// 处理普通文件
}
}
2.11、磁盘空间查询
函数原型:
space_info disk_info(const path& p);
返回值:
- capacity 总容量
- free 可用空间
- available 实际可用空间
示例:
auto info = fs::space("/");
std::cout << "Free: " << info.free << " bytes\n";
3、文件类型检测
| is_block | 判断块设备 |
| is_char | 判断字符设备 |
| is_dir | 判断目录 |
| is_empty | 判断空文件 |
| is_pipe | 判断管道 |
| is_other | 判断其他类型 |
| is_reg | 判断普通文件 |
| is_sock | 判断套接字 |
| is_link | 判断链接 |
| status_ok | 状态有效性检查 |
3.1、块设备检测
函数原型:
bool is_block(const path& p);
示例:
if (fs::is_block("/dev/sda")) {
// 处理块设备
}
3.2、字符设备检测
函数原型:
bool is_char(const path& p);
示例:
if (fs::is_char("/dev/tty")) {
// 处理终端设备
}
3.3、目录检测
函数原型:
bool is_directory(const path& p);
示例:
if (fs::is_directory("my_folder")) {
// 处理目录
}
3.4、空文件检测
函数原型:
bool is_empty(const path& p);
示例:
if (fs::is_empty("empty_file")) {
// 处理空文件
}
3.5、管道检测
函数原型:
bool is_pipe(const path& p);
示例:
if (fs::is_pipe("/tmp/pipe")) {
// 处理命名管道
}
3.6、其他类型检测
函数原型:
bool is_other(const path& p);
示例:
if (fs::is_other("special_file")) {
// 处理特殊文件
}
3.7、普通文件检测
函数原型:
bool is_regular(const path& p);
示例:
if (fs::is_regular("data.txt")) {
// 处理普通文件
}
3.8、套接字检测
函数原型:
bool is_socket(const path& p);
示例:
if (fs::is_socket("/tmp/socket")) {
// 处理套接字文件
}
3.9、链接检测
函数原型:
bool is_link(const path& p);
示例:
if (fs::is_link("link_file")) {
// 处理链接文件
}
3.10、状态有效性检查
函数原型:
bool is_valid(const file_status& st);
示例:
auto st = fs::status("file.txt");
if (fs::is_valid(st)) {
// 处理有效状态
}