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); 
}

4.5
10
IllusiveBrian 18110 points

                                    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**

4.5 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c++ check if a file exists C++ does a file exist check if file exists c+= check if file exists in C++ using filesystem check if file exists c++ using fstream c++ fstream check if file exists verify if file exists c++ detect if file exists c++ detect if file exists c+ find file exists c++ c++ check if text file exists how to check if a file is exists in c++ cpp filesystem check if file exists c++ check file and folder exist c++ check if file name exists how to check if file exits cpp (inside folder) c++ function to check if file exists c++ linux check if file exists c++ know if file exists how to implement check on the file in c++ how to check if file exists in c++ C++ see if a file exists if file exist c++ chect if file exists in c++ check if file exists c++ filesystem how to check if a file exists or not c++ c++ get if file exists howt to check if file exists c++ how to check if a file exists in c++ file handling exist file in c++ check for file exist in c++ how to check if a file exsit in cpp how to check file exist or not in c++ c++ check if a file exists beginners check if filename exists c++ how to check if a file exists in c++ check if file exist or not c++ c++ exist file c++ file exist test if file exist c++ check file exists c++ check for file exist c++ c++ check if file exists in directory filesystem check if file exists c++ how to check file exists in cpp cpp check if file exists fstream c++ check file exists cpp check file exists ceck if a gile exists c++ how to check if a file exists in cpp how to check if a file exsists before opening it in c++ see if file exists in directory c++ check if a file exists in c++ check if some file exits in c++ check file exist c++ c++ check if file exits c++ does file exist know if a file exists cpp how to check if there is an information in a file in cpp check if name file is .c cpp how to see if a file exists c++ how to check if file already exists in c++ c++ check if can access file check if file exists in c++ in if check if file exists in c++ check if a file exists c++ c++ how to check if a file exists cpp check file exist c++ check if file name exiist check if file is folder ifstream check if it is file cpp cpp 14 check if path is file check if is file cpp cpp check if file exist test if a file exists c++ bash if found file is .cpp check if file exists in current directory c++ c++ check if file exists without opening c++ check file exist if file exists C++ how to check if file exists c++ dirent check if file exists c++ c++ check if files exists cp file if exists check file exists in cpp check file exists in c++ cpp check if file exists how to check if a file exists c++ check if a file exists cpp see if file exists c++ c++ check whether file exists c++ check if file exists c++ test if file exists c++ file exists file exist c++ c++ if file exists check if file exists C++ check if file exist in c++ check if file exists cpp
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source