SzH said:
I meant the following definition of "same": Two files are the same if
when one is changed (e.g. written to), the other changes too. I did
not mean two files with the same content, but the same physical file
on the hard disk.
Suppose that we have a utility that reads from one file and writes to
the other. If by accident the same output file is used for both input
and output, then the contents of the file might get deleted, and the
data might be lost ... A simple check is to compare the file names,
but as I explained in the original message, this is not reliable.
On the file systems complying to UNIX conventions (which is where
harlinks are mostly met), you could compare the file system and inode.
Now, a perfect comparison of file systems is a challenge in itself but
often you can reasonably know the files belong to the same file system
(if they are in the same directory, for example, and not symlinks).
Again, on UNIX, not only "disk" files but also others (including
/dev/null mentioned by someone) have a unique inode (within the file
system). Non-UNIX systems must have their own specific convention for
swufid (system-wide unique file id); Windows is an interesting hybrid
in this regard.
All this said, even within UNIX conventions you may disguise the
"sameness" of the files (one easy way is to mount the same NFS file
system more than once, addressing it by IPs of different NICs of the
same file server). Difficult to believe but I saw things like this
happening in production IT environment.
So, to summarize, the problem is most probably unsolvable in general,
even for a single platform, unless the computer system management
follows some rules; if they do, you have to write your own C++ "file
identity comparator" based on those specific rules. Which might be good
for a C++ developer's job security as s/he will always have something to
do
.