Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

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

I would want to discover the quickest method to verify if a file exists in standard C++11, C++, or C. I have thousands of files and before preparing something on them I need to examine if all of them exist. What can I write rather than / SOMETHING */ in the subsequent function?

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

2 Answers

rahul07
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 since C++17, only:

std::filesystem::exists("helloworld.txt");
RoliMishra
I use this piece of code, it works OK with me so far.

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

Login / Signup to Answer the Question.