What’s the best way to check if a file exists in C++? (cross platform)?

Use boost::filesystem: if (!boost::filesystem::exists( "myfile. Txt" ) ) { std::cout.

6 Seems to be a bit of a hazzle to install a huge third party library to do something that should be simple – c0m4 Nov 6 '08 at 9:36 4 Boost is a library where much of what will eventually be a part of C++ standard library is developed. Many of the people involved with boost are people involved with the C++ standard. So boost isn't just any third party library.

If you're programming in C++ you should have boost installed! – Andreas Magnusson Nov 6 '08 at 9:48 @Andreas Magnusson; I would vote you up for this comment! FULL ACK!

– Peter Parker Nov 6 '08 at 10:10 Thanks, I love boost too! – Andreas Magnusson Nov 6 '08 at 10:18 I seem to recall that b::fs::exists returns "true" on non-existent files on network shares: "\\machine\share\this_file_doesnt_exist" => true. Last time I checked was on boost 1.33, use caution... – rlerallut Nov 6 '08 at 12:54.

Be careful of race conditions: if the file disappears between the "exists" check and the time you open it, your program will fail unexpectedly. It's better to go and open the file, check for failure and if all is good then do something with the file. It's even more important with security-critical code.

Details about security and race conditions: ibm.com/developerworks/library/l-sprace.....

I would reconsider trying to find out if a file exists. Instead, you should try to open it (in Standard C or C++) in the same mode you intend to use it. What use is knowing that the file exists if, say, it isn't writable when you need to use it?

Use stat(), if it is cross-platform enough for your needs. It is not C++ standard though, but POSIX. On MS Windows there is _stat, _stat64, _stati64, _wstat, _wstat64, _wstati64.

– c0m4 Nov 6 '08 at 9:28 and See msdn.microsoft. Com/en-us/library/14h5k7ff(VS.71). Aspx – divideandconquer.

Se Nov 18 '08 at 10:35.

To help with your problems googling, try adding "man page" to the search - if you're on Linux, the "man" command is your friend.

Great tip thanks! – c0m4 Nov 6 '08 at 10:20 Haven't you noticed? This site is for people who don't have Google.

– fizzer Nov 6 '08 at 15:40 3 This site is for people fed up with Google bringing back wrong or stupid answers. – Neil May 14 '09 at 19:35.

I am a happy boost user and would certainly use Andreas' solution. But if you didn't have access to the boost libs you can use the stream library: ifstream file(argv1); if (!file) { // Can't open file } It's not quite as nice as boost::filesystem::exists since the file will actually be opened...but then that's usually the next thing you want to do anyway.

1 But with this code you would also jump into the if clause if you don't have permissions for the file, although it exists. In most cases it won't matter, but still worth mentioning. – URL1 Jan 26 at 8:05.

Include if (_access(filename, 0) == -1) { // File does not exist }.

Is io. H normaly available on windows and linux even if its not standard? – c0m4 Nov 6 '08 at 10:30 access() is POSIX function that is available via on Linux.

– Alex B Nov 6 '08 at 13:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions