The C++ Standard Library Filesystem library, added in C++17 and declared in the <filesystem> header, is a portable API for working with paths, files and directories. It provides std::filesystem::path for path manipulation, functions to create, copy, rename and remove files, and iterators to walk directory trees.
Everything lives in the std::filesystem namespace, usually aliased as fs. With GCC before version 9 you had to link -lstdc++fs; current compilers need no extra flag.
path is a value type that stores a path string and offers decomposition and building operations. It does not touch the disk, and the path does not need to exist. The / operator joins components using the right separator for the platform.
Streaming a path prints it in quotes. Use p.string() for a plain std::string, or p.u8string() when you need UTF-8. On Windows, path stores wide characters natively, so prefer path over raw strings when passing file names around.
Use fs::exists, fs::is_regular_file, fs::is_directory and fs::file_size to inspect entries. fs::status returns a file_status with the type and permissions, and fs::last_write_time returns a file_time_type.
Checking exists and then acting is a race condition, since another process can change the file in between. For anything that matters, perform the operation and handle the failure.
| Function | Purpose |
|---|---|
create_directory(p) | Creates one directory; parent must exist |
create_directories(p) | Creates the whole chain, like mkdir -p |
copy_file(from, to, options) | Copies one file; use copy_options::overwrite_existing to replace |
copy(from, to, options) | Copies files or whole trees with copy_options::recursive |
rename(old, new) | Renames or moves within the same file system |
remove(p) | Deletes a file or an empty directory |
remove_all(p) | Deletes recursively and returns the number of items removed |
resize_file, create_symlink, create_hard_link | Resize and link operations |
The four entries are demo, demo/data, a.txt and c.txt. Be careful with remove_all: pointing it at the wrong path deletes a whole tree without confirmation.
directory_iterator lists one level. recursive_directory_iterator walks the whole tree. Each element is a directory_entry, which caches status information so repeated queries are cheap.
Iteration order is unspecified, so sort the results if you need stable output. Without skip_permission_denied, an unreadable directory raises an error.
Most functions come in two overloads. The first throws std::filesystem::filesystem_error, which exposes path1(), path2() and code(). The second takes an std::error_code& and never throws.
Use the error code overloads in loops and performance-sensitive code, and exceptions when a failure should abort the whole operation.
C++17. It grew out of Boost.Filesystem and the Filesystem Technical Specification. Later standards added small refinements.
No. A path is only a string with structure. Use fs::exists or the operation itself to test the disk.
Call p.extension(), which returns a path including the dot, or an empty path when there is none. Use p.stem() for the name without the last extension.
Concurrent reads of separate objects are fine, but operations on the same file system entries race with each other and with other processes. Coordinate access yourself.
The <filesystem> header gives portable C++17 tools for paths, queries, copying, renaming, deleting and directory traversal. Build paths with the / operator, prefer attempting an operation to checking first, use recursive_directory_iterator for tree walks, and pick the throwing or std::error_code overload to suit your error handling.