Use Manipulation Functions with Path

Learn to use manipulation functions with path.

The filesystem library includes functions for manipulating the contents of path objects. In this recipe, we will consider a few of these tools.

How to do it

In this recipe, we examine some functions that manipulate the contents of path objects:

  • We start with the namespace directive and our formatter specialization. We do this in every recipe in this chapter:

namespace fs = std::filesystem;
template<>
struct std::formatter<fs::path>: std::formatter<std::string> {
template<typename FormatContext>
auto format(const fs::path& p, FormatContext& ctx) {
return format_to(ctx.out(), "{}", p.string());
}
};
  • We can get the current working directory with the current_path() function, which returns a path object:

cout << format("current_path: {}\n", fs::current_path());

Output:

current_path: /usr/include
  • The ...

Get hands-on with 1400+ tech skills courses.