Skip to content

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().

#include <filesystem>

Core API Quick Reference ​

OperationSignatureDescription
Path classclass pathPath construction, concatenation, and decomposition (cross-platform separator handling)
Path concatenationpath operator/(const path& lhs, const path& rhs)p / "subdir" / "file.txt"
Current pathpath current_path()Gets/sets the working directory
Directory iterationclass directory_iteratorIterates a single-level directory
Recursive iterationclass recursive_directory_iteratorRecursively iterates subdirectories
File statusbool exists(const path& p)Checks whether a path exists
File sizeuintmax_t file_size(const path& p)Gets the file size in bytes
Create directorybool create_directory(const path& p)Creates a single directory
Create multi-level directoriesbool create_directories(const path& p)Recursively creates the entire path
Copy filebool copy_file(const path& from, const path& to)Copies a single file
Deletebool remove(const path& p)Deletes a file or an empty directory
Recursive deleteuintmax_t remove_all(const path& p)Recursively deletes a directory and its contents
Renamevoid 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 ​

GCCClangMSVC
8719.12

See Also ​


Some content references cppreference.com, used under the CC-BY-SA 4.0 license

pdf-latest-4-g85128cc · 85128cc · 2026-10-05