Bill Kotsias
Bill Kotsias

Reputation: 3368

How to determine if 2 paths reference the same file in portable C++

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

Answers (1)

Stypox
Stypox

Reputation: 1200

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.

Upvotes: 3

Related Questions