Horje
c++ file exists Code Example
check file exist cpp
#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); 
}
c++ file exists
/*-------------------------------------------------------------------
    Returns a boolean indicating whether a file exists 
-------------------------------------------------------------------*/
bool FileExists(string Filepath)
{
   struct stat buffer;
   return (stat (Filepath.c_str(), &buffer) == 0);
}
check file exist cpp
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**




Cpp

Related
c++ pause Code Example c++ pause Code Example
how to use comparator funtion in priority queue in c++ Code Example how to use comparator funtion in priority queue in c++ Code Example
how to make crypto Code Example how to make crypto Code Example
infinity c++ Code Example infinity c++ Code Example
BAPS Code Example BAPS Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
11