The only correct way to check if a file exists is to try opening it. The only correct way to check if a file is writable is to try opening it for writing. Everything else is a race condition. (Other API calls may tell you if the file existed some time ago, but even if it did, there might not have been those 15 nanoseconds later when you try to open it, so they are pretty much useless.)
However, if you want to know if a file exists without opening it, just use the boost::filesystem::exists function. But keep in mind that there is a flaw. It does not tell you if the file exists, it tells you if the file exists.
So be careful how you use it. Do not assume that just because the function returned true, this file will exist when you really try to open it.
If you need to know, “I can open this file,” then the only way to find out is to try opening it.
jalf
source share