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

In summary: I used FILE *f = fopen(<filename>,"r"), and test for f!=0; however, it takes a long time to just open the files.The solution is to use the access(...) function to check if the file exists. This function loops through all the files in the directory and checks to see if the file exists. If the file exists, it returns the access level for the file. If the file does not exist, the function returns -1.
  • #1
ahrkron
Staff Emeritus
Science Advisor
Gold Member
760
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.
 
Last edited:
Technology news on Phys.org
  • #2
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.
 
  • #3
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:
  • #4
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:
  • #5
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").
 
  • #6
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!
 
  • #7
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:
  • #8
You also have the posix

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

Not sure it works on *dows, though
 
  • #9
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.
 

1. How can I check if a file exists in C++?

In C++, you can use the std::ifstream class to open a file and check if it exists. You can then use the is_open() function to determine if the file was successfully opened.

2. What is the fastest way to test for the existence of a file in C++?

The fastest way to test for the existence of a file in C++ is to use the std::ifstream class with the is_open() function. This method avoids any unnecessary overhead and provides a quick way to check if a file exists.

3. Can I use the std::ifstream class to check if a file exists and open it at the same time?

Yes, you can use the std::ifstream class to both check if a file exists and open it at the same time. By passing the file path to the class constructor, you can open the file if it exists, or simply check if it exists without opening it.

4. Is it necessary to use the std::ifstream class to test for file existence in C++?

No, it is not necessary to use the std::ifstream class to test for file existence in C++. You can also use the std::filesystem library, which provides a more modern and efficient way to work with files and directories.

5. What is the return type of the is_open() function in C++?

The is_open() function in C++ returns a boolean value (true if the file was successfully opened, false if it was not).

Similar threads

  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
1
Views
942
  • Programming and Computer Science
Replies
4
Views
11K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top