2

With C++17 (or Boost::filesystem), we can get the current path / current working directory using filesystem::current_path(). However - that gives us an absolute path.

We could also use an empty path as the relative current path - sometimes.

But - is it possible to obtain, portably, the equivalent of "." or "./" ? i.e. a non-empty relative current path?

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

2

Use "." for the current directory.

std::filesystem will recognize "." as representing the current directory / path - regardless of the platform you're on. So, it will not just happen to work on Linux/Windows, it is guaranteed to work.

auto relative_current path = std::filesystem::path{"."};

Relevant wording in the standard: fs.path.generic.3.

This answer is basically due to @NathanOliver...

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Here is the relevant standard wording if you don't want to rely on cppreference: https://timsong-cpp.github.io/cppwp/fs.class.path#fs.path.generic-3 – NathanOliver May 13 '21 at 12:47