Reputation: 3368
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.
Upvotes: 2
Views: 704
Reputation: 1200
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.
Upvotes: 3