6
votes

I don't understand what is throwing the exception here with my input file stream. I have done almost the exact thing before without any problems.

std::string accnts_input_file = "absolute_path/account_storage.txt";
std::string strLine;
std::ifstream istream;
istream.exceptions( std::ifstream::failbit | std::ifstream::badbit );

try
{
    istream.open( accnts_input_file.c_str() );

    while( std::getline( istream, strLine ) )
    {
        std::cout << strLine << '\n';
    }

    istream.close();
}
catch( std::ifstream::failure &e )
{
    std::cerr << "Error opening/reading/closing file" << '\n'
              << e.what()
              << std::endl;
}

I only have it printing the line it reads right now to try and track the error. It reads the file, line by line and prints them, then it throws the exception. The exception says basic_ios::clear, which i don't understand. I think it is ifstream::failbit that is throwing the exception, because when I only set ifstream::badbit it doesn't throw an exception, but I can't figure out why. I've also tried 'while( !istream.oef() )', and most other methods, instead of 'while( std::getline( istream, strLine ) )', but i keep getting the same error.

I'm sure it's probably something obvious I'm missing, but any help would be appreciated. Thanks

3
The last call to std::getline( istream, strLine ) when EOF is reached will probably set istream's failbit.πάντα ῥεῖ
Why don't you try to print more information from the exception? Use the what member function, for example.Some programmer dude
You might try printing e.what() in your catch.Fred Larson
@πάντα ῥεῖ - Wouldn't it throw eofbit if eof was reached?anacy
When i put e.what() in the catch statement, it gives me basic_ios::clear. Which i don't understand, i thought clear just cleared your flagsanacy

3 Answers

8
votes

From this std::getline reference:

...
a) end-of-file condition on input, in which case, getline sets eofbit.
...
3) If no characters were extracted for whatever reason (not even the discarded delimiter), getline sets failbit and returns.

That means that on end of file condition, the function sets both eofbit and failbit. And as you asked to get an exception when failbit is set, the library throws an exception.

2
votes

When you try to read from the stream but there is nothing left to read the read operation will fail (= no characters are inserted in the variable denoted by the 2nd argument of getline) the failbit is set and in your case an exception is thrown.

Using while(!ifstream.eof()) will only help if your file doesn't end for example with a newline character. The eofbit is only set if the end of the stream is reached, i.e. every content of the stream was read out. But if the file ends on a newline character the read operation will fail without having the eofbit set before.

0
votes

One of the possible solutions:

bool mygetline( std::istream &in, std::string &s )
{
    try {
        return std::getline( in, s );
    }
    catch( std::ifstream::failure & )
    {
         if( !in.eof() ) throw;
         return false;
    }
}

std::string accnts_input_file = "absolute_path/account_storage.txt";
std::string strLine;
std::ifstream istream;
istream.exceptions( std::ifstream::failbit | std::ifstream::badbit );

try
{
    istream.open( accnts_input_file.c_str() );

    while( mygetline( istream, strLine ) )
    {
        std::cout << strLine << '\n';
    }

    istream.close();
}
catch( std::ifstream::failure &e )
{
    std::cerr << "Error opening/reading/closing file" << '\n'
              << e.what()
              << std::endl;
}