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

  • Context: C/C++ 
  • Thread starter Thread starter ahrkron
  • Start date Start date
  • Tags Tags
    Existence File Test
Click For Summary

Discussion Overview

The discussion revolves around methods for efficiently checking the existence of files in C++. Participants explore various approaches and functions available in different operating systems, including Unix and Windows, while addressing performance concerns related to file handling.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes a situation where checking for file existence using fopen is slow and seeks faster alternatives.
  • Another participant suggests using stat from sys/stat.h to check file existence, noting that it may not be available on Windows.
  • A different approach for Windows is proposed, using System::IO::File::Exists, which returns a boolean indicating file existence.
  • Concerns are raised about differences in function names between Unix and Windows, specifically mentioning _fstat as a Microsoft-specific alternative to stat.
  • A participant clarifies that the return values of stat may be counterintuitive, as it returns -1 for non-existing files and 0 for existing files.
  • Another participant acknowledges that their initial performance issues were due to other I/O operations in their code, not the file existence check itself.
  • Additional methods are mentioned, such as using access from unistd.h, though its compatibility with Windows is questioned.
  • A later reply humorously points out that some responses are to older posts, emphasizing the ongoing relevance of checking file existence.

Areas of Agreement / Disagreement

Participants present multiple competing views on the best method to check for file existence, with no consensus reached on a single preferred approach. There is acknowledgment of the differences in function availability and behavior across operating systems.

Contextual Notes

Limitations include potential confusion over return values of functions like stat, as well as the need to adapt code when porting between Unix and Windows due to differing function names.

ahrkron
Staff Emeritus
Science Advisor
Gold Member
Messages
755
Reaction score
2
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?

Edit: FILE instead of TFILE.[/color]
 
Last edited:
Technology news on Phys.org
How about this:

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.
 
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:
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

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:
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").
 
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!
 
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

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:
You also have the posix

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

Not sure it works on *dows, though
 
Um... did you notice that the posts you responded to are six and a half years old? :smile:
 
  • #10
jtbell said:
Um... did you notice that the posts you responded to are six and a half years old? :smile:
That only makes it only more important to check if the file still exists.
 

Similar threads

  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 4 ·
Replies
4
Views
5K