本文翻译自:Fastest way to check if a file exist using standard C++/C++11/C?
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. 我想找到检查文件是否存在于标准C ++ 11,C ++或C中的最快方法。我有数千个文件,在对它们做某事之前我需要检查它们是否全部存在。 What can I write instead of /* SOMETHING */
in the following function? 在下面的函数中我可以编写什么而不是/* SOMETHING */
?
inline bool exist(const std::string& name)
{
/* SOMETHING */
}
#1楼
参考:https://stackoom.com/question/rb9b/使用标准C-C-C检查文件是否存在的最快方法
#2楼
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"). 单独检查每个文件的最快方法取决于您的系统...如果您正在编写ANSI C,最快的方法是fopen
因为它是唯一的方式(文件可能存在但不可打开,但您可能真的想要打开你需要“做点什么”)。 C++, POSIX, Windows all offer additional options. C ++,POSIX,Windows都提供了其他选项。
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). 你说你想要最快的方式,并且你有数千个文件,但是你要求函数的代码来测试单个文件(并且该函数仅在C ++中有效,而不是C)。 This contradicts your requirements by making an assumption about the solution ... a case of the XY problem . 这通过对解决方案做出假设而与您的要求相矛盾......这是XY问题的一个例子。 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. 你还说“在标准的c ++ 11(或)c ++(或)c”......这些都是不同的,这也与你对速度的要求不一致......最快的解决方案是将代码定制到目标系统。 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++. 问题的不一致性在于您接受了一个答案,该答案提供了依赖于系统且不是标准C或C ++的解决方案。
#3楼
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. 好吧,我把一个测试程序放在一起,运行每个方法100,000次,一半是存在的文件,一半是没有的文件。
#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <fstream>
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, 运行100,000次呼叫的总时间平均超过5次,
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. stat()
函数在我的系统上提供了最佳性能(Linux,使用g++
编译),如果由于某种原因拒绝使用POSIX函数,标准fopen
调用是最好的选择。
#4楼
I use this piece of code, it works OK with me so far. 我使用这段代码,到目前为止它对我来说还可以。 This does not use many fancy features of C++: 这并没有使用C ++的许多奇特功能:
bool is_file_exist(const char *fileName)
{
std::ifstream infile(fileName);
return infile.good();
}
#5楼
inline bool exist(const std::string& name)
{
ifstream file(name);
if(!file) // If the file was not found, then file is 0, i.e. !file=1 or true.
return false; // The file was not found.
else // If the file was found, then file is non-0.
return true; // The file was found.
}
#6楼
Same as suggested by PherricOxide but in C 与PherricOxide建议的相同,但在C中
#include <sys/stat.h>
int exist(const char *name)
{
struct stat buffer;
return (stat (name, &buffer) == 0);
}