The Path Object
Explore how to use the C++17 filesystem path object to manipulate file paths. Understand its components such as root name, directory, relative path, filename, stem, and extension. Learn methods to query, modify, and compare paths on Windows and POSIX systems, helping you manage file system paths efficiently.
We'll cover the following...
We'll cover the following...
The core part of the library is the path object. It contains a pathname - a string that forms the name of the path. The object doesn’t have to point to an existing file in the filesystem. The path might be even in an invalid form.
The path is composed of the following elements:
root-name root-directory relative-path:
- (optional) root-name: POSIX systems don’t have a root name. On Windows, it’s usually the name of a drive, like
"C:" - (optional) root-directory: distinguishes relative path from the absolute path
- relative-path:
- filename
- directory separator
- relative-path
We can illustrate it with the following diagram:
The class implements a lot of methods that extracts the parts of the path:
| Method | Description |
|---|---|
path::root_name() |
returns the root-name of the path |
path::root_directory() |
returns the root directory of the path |
path::root_path() |
returns the root path of the path |
path::relative_path() |