Show Menu
Cheatography

C++ (std::filesystem) Cheat Sheet (DRAFT) by

A cheatsheet for for std::filesystem

This is a draft cheat sheet. It is a work in progress and is not finished yet.

std::f­ile­sys­tem­::path

std::filesystem::path my_path = "/some/directory";
std::filesystem::path sub_path = my_path / "subdirectory";  // appends to path
std::filesystem::path file_path = my_path / "file.txt";
Represents a filesystem path in a cross-­pla­tform way, handling differ­ences between operating systems automa­tic­ally.

Can be constr­ucted from a string and supports path operations like appending with /.

Part of the std::f­ile­system library introduced in C++17.

std::f­ile­sys­tem­::r­emo­ve_­all()

std::filesystem::remove_all(directory_path);          // throws on error
std::filesystem::remove_all(directory_path, ec);      // stores error in ec, no throw
Removes a file OR a directory plus all of its contents recurs­ively.

Returns the number of files and direct­ories removed.

Accepts an optional std::e­rro­r_code parameter to handle errors without throwing except­ions.

std::f­ile­sys­tem­::e­xists

std::filesystem::exists("/tmp/my_file.txt");   // true if file exists
std::filesystem::exists("/tmp/my_directory");  // true if directory exists
std::filesystem::exists("/tmp/missing");       // false if nothing there
Checks whether a file or directory exists at the given path.

Returns true if something exists at that path, false if it doesn't.

Commonly used in tests to verify files were created or deleted.
 

std::f­ile­sys­tem­::p­erm­issions

std::filesystem::permissions(
    file_path,
    std::filesystem::perms::owner_all      | // owner can read, write, execute
    std::filesystem::perms::group_read     | // group can read
    std::filesystem::perms::group_exec     | // group can execute
    std::filesystem::perms::others_read    | // others can read
    std::filesystem::perms::others_exec      // others can execute
);
Sets the access permis­sions on a file or directory.

Permis­sions control who can read, write, or execute a file.

Uses std::f­ile­sys­tem­::perms flags combined with | to set multiple permis­sions at once.

std::f­ile­sys­tem­::c­rea­te_­dir­ect­ories()

std::filesystem::create_directories("/tmp/my_dir/sub_dir/another");
// creates all three directories if they don't exist

std::filesystem::create_directory("/tmp/my_dir");
// only creates my_dir, fails if /tmp doesn't exist
Creates a directory and any missing parent direct­ories in the path.

Does nothing if the directory already exists.

Use create­_di­rec­tory() instead if you only need to create a single directory with an existing parent.
 

std::f­ile­sys­tem­::t­emp­_di­rec­tor­y_p­ath()

std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
// temp_dir == "/tmp" on Linux

// commonly used to build a unique temp path
std::filesystem::path my_temp = std::filesystem::temp_directory_path() / "my_temp_dir";
// my_temp == "/tmp/my_temp_dir"
Returns the path to the system's temporary directory.

On Linux this is typically /tmp, on Windows it's usually C:\Use­rs­\use­rna­me­\App­Dat­a\L­oca­l\Temp.

Useful for creating temporary files and direct­ories during tests that won't interfere with the real filesy­stem.

std::f­ile­sys­tem­::p­ath­::s­tring()

std::filesystem::path my_path = "/tmp/my_directory";

std::string path_string = my_path.string();  // "/tmp/my_directory"

// commonly needed when passing to C functions
setenv("MY_VAR", my_path.string().c_str(), 1);
Converts a std::f­ile­sys­tem­::path to a std::s­tring.

Necessary when passing a path to functions that expect a plain string rather than a fs::path object.

Returns the path as a native string repres­ent­ation for the current operating system.