# Fast way to test for existence of a file in C++?

#### ahrkron

Staff Emeritus
Gold Member
Here's the situation:

I'm running over a few thousand files, some of which may have already been processed. In order to decide if a file has to be processed, I check if the corresponding output file exists.

The problem:

I tried using FILE *f = fopen(<filename>,"r"), and test for f!=0; however, it takes a long time to just open the files.

Any suggestions?

Last edited:

#### dduardo

##### Technology Mentor
Staff Emeritus

Code:
#include <sys/stat.h>

int fexist( char *filename ) {
struct stat buffer ;
if ( stat( filename, &buffer ) ) return 1 ;
return 0 ;
}
This is under Unix, so stat.h may not be under sys/ for windows.

#### ramollari

If you're programming for Windows check if the following works:

Code:
System::IO::File::Exists(file);
which returns true if the file exists, false otherwise.

Last edited:

#### dduardo

##### Technology Mentor
Staff Emeritus
Here is the msdn docmentation for stat.h:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__fstat.2c_._fstati64.asp [Broken]

It looks microsoft is up to their dirty tricks again. Instead of calling their function stat(....), they conveniently change it to _fstat(....). If anyone wants to port their code to unix they would need to change their function calls. Nice job. Even borland uses stat(...).

Last edited by a moderator:

#### ahrkron

Staff Emeritus
Gold Member
Thanks!

That is fast enough.
Just it seems to have the logic reversed: stats(...) returns -1 for non-existing files and 0 for existing files.

(or it may be my expectation of 1 to mean "true" and 0 to be "does not exist").

#### ahrkron

Staff Emeritus
Gold Member
OK. I was just being dumb. I had a piece of code before the actual test of file existence that was using some serious IO. Now both versions (dduardo's and my original FILE use) work ok.

Thanks anyway!

#### master_coda

dduardo said:
Here is the msdn docmentation for stat.h:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__fstat.2c_._fstati64.asp [Broken]

It looks microsoft is up to their dirty tricks again. Instead of calling their function stat(....), they conveniently change it to _fstat(....). If anyone wants to port their code to unix they would need to change their function calls. Nice job. Even borland uses stat(...).
But you code you provided will work with Microsoft's compiler.

Last edited by a moderator:

#### verwall

You also have the posix

#include <unistd.h>
int access(const char *path, int amode);

Not sure it works on *dows, though

#### jtbell

Mentor
Um... did you notice that the posts you responded to are six and a half years old?

#### Timo

Um... did you notice that the posts you responded to are six and a half years old?
That only makes it only more important to check if the file still exists.

### The Physics Forums Way

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving