However, in theory, this IS the way to go to check if a file exists IF you want to read it as well!

The reason is that otherwise you might end up with a race condition: You check if the file exists and then open the file. Between the check and you opening the file, the file gets removed and you end up trying to open a file that doesn't exist (which returns an invalid handle).

The right way to do it is to open the file, which checks if it exists anyway, and then to check the returned file handle. And as Chaos pointed out, don't forget to close the files once you are done reading them, or you'll leak file handles.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com