What Is Throwing The Exception In This File Stream?

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


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.


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, ie 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.


可能的解决方案之一:

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;
}
链接地址: http://www.djcxy.com/p/61308.html

上一篇: 在Windows上使用fopen目录

下一篇: 什么是在这个文件流中抛出异常?