Chapter Overview

Let's get an idea of what we'll cover in this chapter.

We'll cover the following...

The purpose of the STL filesystem library is to normalize file system operations across platforms. The filesystem library seeks to normalize operations, bridging irregularities between POSIX/Unix, Windows, and other file systems.

The filesystem library was adopted from the corresponding Boost library and incorporated into the STL with C++17. At the time of writing, there are still gaps in its implementation on some systems, but the recipes in this chapter have been tested on Linux, Windows, and macOS file systems, and compiled with the latest available versions of the GCC, MSVC, and Clang compilers, respectively.

The library uses the <filesystem> header, and the std::filesystem namespace is commonly aliased as fs:

namespace fs = std::filesystem;

The fs::path class is at the core of the filesystem library. It provides normalized filename and directory path representation across disparate environments. A path object may represent a file, a directory, or any object, even in a non-existent or impossible object.

In the following recipes, we cover tools for working with files and directories using the filesystem library:

  • Specialize std::formatter for the path class
  • Use manipulation functions with path
  • List files in a directory
  • Search directories and files with a grep utility
  • Rename files with regex and directory_iterator
  • Create a disk usage counter