|
| 1 | +--- |
| 2 | +title: "std::filesystem" |
| 3 | +description: "跨平台的文件系统操作库:路径操作、目录遍历、文件状态查询" |
| 4 | +chapter: 99 |
| 5 | +order: 6 |
| 6 | +tags: |
| 7 | + - host |
| 8 | + - cpp-modern |
| 9 | + - beginner |
| 10 | +difficulty: beginner |
| 11 | +cpp_standard: [17, 20, 23] |
| 12 | +--- |
| 13 | + |
| 14 | +<!-- |
| 15 | +参考卡模板 (Reference Card Template) |
| 16 | +用于 documents/cpp-reference/ 下的特性速查页。 |
| 17 | +与 article-template.md 不同,参考卡走精炼的结构化格式,不需要叙事风格。 |
| 18 | +
|
| 19 | +标签使用规则: |
| 20 | +1. 必须包含 1 个 platform 标签(参考卡统一用 host) |
| 21 | +2. 必须包含 1 个 difficulty 标签 |
| 22 | +3. 至少包含 1 个 topic 标签 |
| 23 | +4. 从 scripts/validate_frontmatter.py 的 VALID_TAGS 集合中选取 |
| 24 | +--> |
| 25 | + |
| 26 | +# std::filesystem(C++17) |
| 27 | + |
| 28 | +## 一句话 |
| 29 | + |
| 30 | +平台无关的文件系统操作库:路径拼接与规范化、目录创建与遍历、文件复制与删除、权限与状态查询——告别 `stat()` 和 `opendir()`。 |
| 31 | + |
| 32 | +## 头文件 |
| 33 | + |
| 34 | +`#include <filesystem>` |
| 35 | + |
| 36 | +## 核心 API 速查 |
| 37 | + |
| 38 | +| 操作 | 签名 | 说明 | |
| 39 | +|------|------|------| |
| 40 | +| 路径类 | `class path` | 路径的构造、拼接、分解(跨平台分隔符处理) | |
| 41 | +| 路径拼接 | `path operator/(const path& lhs, const path& rhs)` | `p / "subdir" / "file.txt"` | |
| 42 | +| 当前路径 | `path current_path()` | 获取/设置工作目录 | |
| 43 | +| 目录迭代 | `class directory_iterator` | 遍历单层目录 | |
| 44 | +| 递归迭代 | `class recursive_directory_iterator` | 递归遍历子目录 | |
| 45 | +| 文件状态 | `bool exists(const path& p)` | 检查路径是否存在 | |
| 46 | +| 文件大小 | `uintmax_t file_size(const path& p)` | 获取文件字节数 | |
| 47 | +| 创建目录 | `bool create_directory(const path& p)` | 创建单个目录 | |
| 48 | +| 创建多级目录 | `bool create_directories(const path& p)` | 递归创建整个路径 | |
| 49 | +| 复制文件 | `bool copy_file(const path& from, const path& to)` | 复制单个文件 | |
| 50 | +| 删除 | `bool remove(const path& p)` | 删除文件或空目录 | |
| 51 | +| 递归删除 | `uintmax_t remove_all(const path& p)` | 递归删除目录及内容 | |
| 52 | +| 重命名 | `void rename(const path& old, const path& newp)` | 重命名或移动 | |
| 53 | + |
| 54 | +## 最小示例 |
| 55 | + |
| 56 | +```cpp |
| 57 | +// Standard: C++17 |
| 58 | +#include <filesystem> |
| 59 | +#include <iostream> |
| 60 | + |
| 61 | +namespace fs = std::filesystem; |
| 62 | + |
| 63 | +int main() { |
| 64 | + fs::path p = fs::current_path() / "test.txt"; |
| 65 | + std::cout << p << "\n"; // 完整路径 |
| 66 | + std::cout << p.filename() << "\n"; // test.txt |
| 67 | + std::cout << p.extension() << "\n"; // .txt |
| 68 | + |
| 69 | + fs::create_directories("a/b/c"); // 递归创建 |
| 70 | + std::cout << fs::exists("a/b") << "\n"; // true |
| 71 | + fs::remove_all("a"); // 递归删除 |
| 72 | +} |
| 73 | +``` |
| 74 | +
|
| 75 | +## 嵌入式适用性:低 |
| 76 | +
|
| 77 | +- 依赖操作系统文件系统抽象层(POSIX 或 Win32),裸机环境无文件系统 |
| 78 | +- 适用于嵌入式 Linux(如 Buildroot/Yocto 平台)或上位机配置/日志工具 |
| 79 | +- 头文件引入的开销较大,资源极度受限的设备不建议使用 |
| 80 | +- 对需要文件系统的嵌入式场景(如 FAT32 on SD card),可选用轻量级替代(如 LittleFS) |
| 81 | +
|
| 82 | +## 编译器支持 |
| 83 | +
|
| 84 | +| GCC | Clang | MSVC | |
| 85 | +|-----|-------|------| |
| 86 | +| 8 | 7 | 19.12 | |
| 87 | +
|
| 88 | +## 另见 |
| 89 | +
|
| 90 | +- [教程:std::filesystem](../../vol2-modern-features/ch09-filesystem/01-filesystem-path.md) |
| 91 | +- [cppreference: std::filesystem](https://en.cppreference.com/w/cpp/filesystem) |
| 92 | +
|
| 93 | +--- |
| 94 | +
|
| 95 | +*部分内容参考自 [cppreference.com](https://en.cppreference.com/),采用 [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) 许可* |
0 commit comments