Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is the C++ Filesystem Library (std::filesystem)?

C++
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJul 24, 2023
UpdatedNov 17, 2026
7 min read

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.

Table of Contents

  • Working with std::filesystem::path
  • Querying files and permissions
  • Creating, copying, renaming and removing
  • Listing and walking directories
  • Error handling
  • Frequently Asked Questions
    • Which C++ standard introduced std::filesystem?
    • Does std::filesystem::path check that the file exists?
    • How do I get the file extension?
    • Is std::filesystem thread safe?
  • Summary

Working with std::filesystem::path

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.

cpp
text

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.

Querying files and permissions

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.

cpp

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.

Creating, copying, renaming and removing

FunctionPurpose
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_linkResize and link operations
cpp
text

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.

Listing and walking directories

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.

cpp

Iteration order is unspecified, so sort the results if you need stable output. Without skip_permission_denied, an unreadable directory raises an error.

Error handling

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.

cpp

Use the error code overloads in loops and performance-sensitive code, and exceptions when a failure should abort the whole operation.

Frequently Asked Questions

Which C++ standard introduced std::filesystem?

C++17. It grew out of Boost.Filesystem and the Filesystem Technical Specification. Later standards added small refinements.

Does std::filesystem::path check that the file exists?

No. A path is only a string with structure. Use fs::exists or the operation itself to test the disk.

How do I get the file extension?

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.

Is std::filesystem thread safe?

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.

Summary

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.

Share