正常
std::filesystem (C++17)
TL;DR
A platform-agnostic file system library: path concatenation and normalization, directory creation and traversal, file copying and deletion, permissions and status queries—say goodbye to std::ifstream/std::ofstream and OS APIs.
Header
cpp
#include <filesystem>
namespace fs = std::filesystem;Core API Cheat Sheet
| Operation | Signature | Description |
|---|---|---|
| Path class | std::filesystem::path | Path construction, concatenation, decomposition (handles cross-platform separators) |
| Path concatenation | p / "subdir" | Joins paths with OS-specific separator |
| Current path | fs::current_path | Gets/sets the working directory |
| Directory iteration | fs::directory_iterator | Iterates over a single-level directory |
| Recursive iteration | fs::recursive_directory_iterator | Recursively iterates over subdirectories |
| File status | fs::exists | Checks if a path exists |
| File size | fs::file_size | Gets file size in bytes |
| Create directory | fs::create_directory | Creates a single directory |
| Create multi-level directory | fs::create_directories | Recursively creates the entire path |
| Copy file | fs::copy_file | Copies a single file |
| Delete | fs::remove | Deletes a file or empty directory |
| Recursive delete | fs::remove_all | Recursively deletes a directory and its contents |
| Rename | fs::rename | Renames or moves a file |
Minimal Example
cpp
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main() {
// Create directories
fs::create_directories("sandbox/dir1/dir2");
// Copy file
fs::copy_file("source.txt", "sandbox/source.txt");
// Iterate directory
for (const auto& entry : fs::directory_iterator("sandbox")) {
std::cout << entry.path() << '\n';
}
// Cleanup
fs::remove_all("sandbox");
}Embedded Applicability: Low
- Depends on the OS file system abstraction layer (POSIX or Win32); bare-metal environments lack a file system.
- Suitable for Embedded Linux (e.g., Buildroot/Yocto platforms) or host-side configuration/logging tools.
- Header inclusion overhead is significant; not recommended for resource-constrained devices.
- For embedded scenarios requiring a file system (e.g., FAT32 on SD card), consider lightweight alternatives like LittleFS.
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 8 | 7 | 19.12 |
See Also
Part of the content references cppreference.com, licensed under CC-BY-SA 4.0