Fastest way to check if a file exist using standard C++/C++11/C?

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

I would like to find the fastest way to check if a file exist in standard C++11, C++, or C. I have thousands of files and before doing something on them I need to check if all of them exist. What can I write instead of /* SOMETHING */ in the following function?

inline bool exist(const std::string& name) {     /* SOMETHING */ }

回答1:

Well I threw together a test program that ran each of these methods 100,000 times, half on files that existed and half on files that didn't.

#include  #include  #include   inline bool exists_test0 (const std::string& name) {     ifstream f(name.c_str());     return f.good(); }  inline bool exists_test1 (const std::string& name) {     if (FILE *file = fopen(name.c_str(), "r")) {         fclose(file);         return true;     } else {         return false;     }    }  inline bool exists_test2 (const std::string& name) {     return ( access( name.c_str(), F_OK ) != -1 ); }  inline bool exists_test3 (const std::string& name) {   struct stat buffer;      return (stat (name.c_str(), &buffer) == 0);  }

Results for total time to run the 100,000 calls averaged over 5 runs,

Method exists_test0 (ifstream): **0.485s** Method exists_test1 (FILE fopen): **0.302s** Method exists_test2 (posix access()): **0.202s** Method exists_test3 (posix stat()): **0.134s**

The stat() function provided the best performance on my system (Linux, compiled with g++), with a standard fopen call being your best bet if you for some reason refuse to use POSIX functions.



回答2:

I use this piece of code, it works OK with me so far. This does not use many fancy features of C++:

bool is_file_exist(const char *fileName) {     std::ifstream infile(fileName);     return infile.good(); }


回答3:

Remark : in C++14 and as soon as the filesystem TS will be finished and adopted, the solution will be to use:

std::experimental::filesystem::exists("helloworld.txt");

and hopefully in C++17, only:

std::filesystem::exists("helloworld.txt");


回答4:

It depends on where the files reside. For instance, if they are all supposed to be in the same directory, you can read all the directory entries into a hash table and then check all the names against the hash table. This might be faster on some systems than checking each file individually. The fastest way to check each file individually depends on your system ... if you're writing ANSI C, the fastest way is fopen because it's the only way (a file might exist but not be openable, but you probably really want openable if you need to "do something on it"). C++, POSIX, Windows all offer additional options.

While I'm at it, let me point out some problems with your question. You say that you want the fastest way, and that you have thousands of files, but then you ask for the code for a function to test a single file (and that function is only valid in C++, not C). This contradicts your requirements by making an assumption about the solution ... a case of the XY problem. You also say "in standard c++11(or)c++(or)c" ... which are all different, and this also is inconsistent with your requirement for speed ... the fastest solution would involve tailoring the code to the target system. The inconsistency in the question is highlighted by the fact that you accepted an answer that gives solutions that are system-dependent and are not standard C or C++.



回答5:

For those who like boost:

 boost::filesystem::exists(fileName)


回答6:

Without using other libraries, I like to use the following code snippet:

#ifdef _WIN32    #include      #define access    _access_s #else    #include  #endif  bool FileExists( const std::string &Filename ) {     return access( Filename.c_str(), 0 ) == 0; }

This works cross-platform for Windows and POSIX-compliant systems.



回答7:

Same as suggested by Pherr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!