1

I was wondering if there is a portable way to determine if 2 different paths actually reference the same file.

I have read this thread but is Windows-specific.

AFAIK, fstream isn't suitable for the job.

Community
  • 1
  • 1
Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60
  • Does this answer your question? [What is the best way of determining that two file paths are referring to the same file object?](https://stackoverflow.com/questions/29497131/what-is-the-best-way-of-determining-that-two-file-paths-are-referring-to-the-sam) – Stypox Jun 15 '21 at 16:12

1 Answers1

3

Filesystem library

Since C++17 you can use the standard <filesystem> library. The function you are looking for is equivalent, under namespace std::filesystem:

bool std::filesystem::equivalent(const std::filesystem::path& p1, const filesystem::path& p2 );

To summarize from the documentation: this function takes two paths as parameters and returns true if they reference the same file or directory, false otherwise. There is also a noexcept overload that takes a third parameter: an std::error_code in which to save any possible error.

For more information take a look at my answer on the thread you mentioned.

Stypox
  • 963
  • 11
  • 18