Try-catch block for C ++ IO file errors not working

I am very new to the world of error handling in C ++, but they told me:
Checking file availability in C ++

... that the best way to check for the existence of a file was with a try-catch block. From my limited knowledge on this topic, this sounds like sound advice. I found this piece of code:
http://www.java2s.com/Tutorial/Cpp/0240__File-Stream/Readafileintrycatchblock.htm

#include <fstream> #include <iostream> using namespace std; int main () { try{ char buffer[256]; ifstream myfile ("test.txt"); while (! myfile.eof() ) { myfile.getline (buffer,100); cout << buffer << endl; } }catch(...){ cout << "There was an error !\n"; } return 0; } 

... but when I compile it with

 g++ -Wall -pedantic -o test_prog main.cc 

And run the program in the directory where test.txt does not exist, the program continues to spit out empty lines on the terminal. Can anyone understand why?

Also is this a good way to check if a file exists for a file that you really want to open and read (compared to something where you index a bunch of files and check them)?

Thanks!

+6
c ++ filesystems file-io try-catch error-handling
source share
3 answers

In C ++, iostreams do not throw exceptions by default. You need

 ifstream myfile("test.txt"); if(myfile) { // We have one } else { // we dont } 
+10
source share

By default, fstream objects are not thrown away. To set the exception behavior, you need to use void exceptions ( iostate except ); . You can select the current settings with iostate exceptions ( ) const; . Change the code a bit:

 #include <fstream> #include <iostream> #include <stdexcept> using namespace std; int main () { try{ char buffer[256]; ifstream myfile ("test.txt"); myfile.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit ); while (file) { myfile.getline (buffer,100); cout << buffer << endl; } }catch(std::exception const& e){ cout << "There was an error: " << e.what() << endl; } return 0; } 
+11
source share

First of all, for the try block to do something good, you need to include exceptions for the stream.

Secondly, a loop like:

  while (! myfile.eof() ) 

It will not cause trouble, and you will see it here. The problem (in this case) is that when the file does not open, eof will never be signaled - you cannot / do not go to the end of the file because there is no file. Thus, your cycle runs forever, in an existentialist search for the end of a nonexistent file. Fix the loop and things get better in a hurry:

  char buffer[256]; ifstream myfile ("test.txt"); while (myfile.getline(buffer,100)) { cout << buffer << endl; } 

While you are on it, a little more corrections will not hurt (if you really do not want to use less than half of the space that you allocated for your buffer):

  char buffer[256]; ifstream myfile ("test.txt"); while (myfile.getline(buffer,sizeof(buffer))) { cout << buffer << endl; } 

Or, of course, completely fix the problem:

 std::string buffer; ifstream myfile("test.txt"); while (getline(myfile, buffer)) cout << buffer << "\n"; 

Edit: Note that none of these (at least for the time being) are dependent on exceptions in general. They are configured to write a line to the output if we can read the line from the input. If the file does not open, the body of the loop simply will not execute, because we cannot read from the file that was not opened. If we want to print an error message informing the user that the file does not open, we will have to process it separately from what is indicated above. For example:

 ifstream myfile("test.txt"); if (!myfile) { std::cerr << "File failed to open"; return FAIL; } while (std::getline(myfile // ... 
+8
source share

All Articles