Does a try-catch block catch segmentation fault errors?
I am reading a text file using the function given below but sometimes the file is empty and the
You can try to add a little test to see if ifs is valid:
#include
#include
int main(int argc, char * argv[]){
std::ifstream ifs( "notExists" );
if( ifs.is_open()) {
char line[1024];
while(! ifs.fail() && ifs.getline( line, sizeof( line ))) {
std::cout << line << std::endl;
}
}
else {
std::cout << "file doesn't exists." << std::endl;
}
std::cout << "done." << std::endl;
return 0;
}
This program runs and output:
file doesn't exists.
done.
bool std::ifstream::is_open();
See the getline global function for more information, if it fail, it set some bit not checked here.
Errors are signaled by modifying the internal state flags:
Additionally, in any of these cases, if the appropriate flag has been set with is's member function ios::exceptions, an exception of type ios_base::failure is thrown.