cpp check if file opened code example
Example 1: c++ check if file exits
#include <fstream>
#include<iostream>
using namespace std;
int main() {
/* try to open file to read */
ifstream ifile;
ifile.open("b.txt");
if(ifile) {
cout<<"file exists";
} else {
cout<<"file doesn't exist";
}
}
Example 2: how to check if file is opened c++
ifstream file;
file.open(argv[1]);
if (!file.is_open()) {
cerr << "File did not open";
exit(1);
}