What is the best way of determining that two file paths are referring to the same file object?

You could check out the Boost.Filesystem library. Specifically, there is a method equivalent that seems to do exactly what you are looking for:

using namespace boost::filesystem;

path p("/path/to/file/one");
path q("/sym_link/to/one");
assert(equivalent(p, q));

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 complete answer to another stack overflow question.

Tags:

Windows

Linux

C++

C