user788171
user788171

Reputation: 17553

c++, fastest way to check if a file exists?

There are multiple ways to check if a file exists.

The options I know of are:

  1. boost::filesystem exists()
  2. access()
  3. stat()
  4. ifstream is_open()

Does anybody know which of these gives the highest performance?

EDIT: Assume running on /dev/shm where access time is not a factor.

Upvotes: 3

Views: 1859

Answers (2)

Amit G.
Amit G.

Reputation: 2684

Modern C++ | Update

C++17 provides this std function:

#include <filesystem>

int main()
{
    std::filesystem::path FullPath("C:\\Test.txt");

    bool bExists = std::filesystem::exists(FullPath);
}

Upvotes: 2

Puppy
Puppy

Reputation: 147036

The runtime here will be dominated by the switch into kernel mode and operation of the filesystem driver- even ignoring disk time. It's highly unlikely that any of them will offer superior performance. Best to pick the one which offers the best interface- boost::filesystem.

Upvotes: 7

Related Questions