正常
std::filesystem (C++17)
In a Nutshell
A platform-independent filesystem library: path concatenation and normalization, directory creation and traversal, file copying and deletion, permission and status queries — say goodbye to stat() and opendir().
Header
#include <filesystem>
Core API Quick Reference
| Operation | Signature | Description |
|---|---|---|
| Path class | class path | Path construction, concatenation, and decomposition (cross-platform separator handling) |
| Path concatenation | path operator/(const path& lhs, const path& rhs) | p / "subdir" / "file.txt" |
| Current path | path current_path() | Gets/sets the working directory |
| Directory iteration | class directory_iterator | Iterates a single-level directory |
| Recursive iteration | class recursive_directory_iterator | Recursively iterates subdirectories |
| File status | bool exists(const path& p) | Checks whether a path exists |
| File size | uintmax_t file_size(const path& p) | Gets the file size in bytes |
| Create directory | bool create_directory(const path& p) | Creates a single directory |
| Create multi-level directories | bool create_directories(const path& p) | Recursively creates the entire path |
| Copy file | bool copy_file(const path& from, const path& to) | Copies a single file |
| Delete | bool remove(const path& p) | Deletes a file or an empty directory |
| Recursive delete | uintmax_t remove_all(const path& p) | Recursively deletes a directory and its contents |
| Rename | void rename(const path& old, const path& newp) | Renames or moves |
Minimal Example
C++
// Standard: C++17
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main() {
fs::path p = fs::current_path() / "test.txt";
std::cout << p << "\n"; // full path
std::cout << p.filename() << "\n"; // test.txt
std::cout << p.extension() << "\n"; // .txt
fs::create_directories("a/b/c"); // recursive creation
std::cout << fs::exists("a/b") << "\n"; // true
fs::remove_all("a"); // recursive deletion
}Embedded Applicability: Low
- Depends on the operating system's filesystem abstraction layer (POSIX or Win32); bare-metal environments have no filesystem
- Suitable for embedded Linux (e.g., Buildroot/Yocto platforms) or host-side configuration/logging tools
- The header carries considerable inclusion overhead; not recommended for extremely resource-constrained devices
- For embedded scenarios that do need a filesystem (e.g., FAT32 on an SD card), consider a lightweight alternative such as LittleFS
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 8 | 7 | 19.12 |
See Also
Some content references cppreference.com, used under the CC-BY-SA 4.0 license