Hi,
I am GS again; in addition to my question of yesterday I made some
further attempts: here follows an updated version of my sample code:
// code begins
# include <iostream>
# include <fstream>
# include <sstream>
# include <exception>
# include <system_error>
using namespace std;
int main( )
{
istringstream is("abcdefghijklmnopqrstuvwxyz");
//ifstream is("file"); // with the same content
cout << "begins: " << is . tellg( ) << '\n';
char c = is.get( );
cout << "after get " << is . tellg( ) << " with " << is . gcount( )
<< " and c is [" << c << "]\n";
is.seekg(2, ios::cur); // move ahead two bytes
cout << "after seekg " << is . tellg( ) << " with " << is . gcount( )
<< '\n';
cout << "STATE A " << is.rdstate() << '\n';
is . putback(c); // shouldn't set badbit ??
try
{
is . exceptions(is . badbit);
// moreover neither this exception is thrown
}
catch(ios_base :: failure & e)
{cout << "caught!\n";
cout << e.what() << '\n' /*<< e.code() << '\n'*/;
// and in addition ios_base::failure objects seem NOT to inherit
// the function code() from std :: system_error
// (that's why it is commented out): what happens?
}
cout << "STATE B " << is.rdstate() << '\n';
// it turns out that badbit is not set (same output [0] in STATE A as
in STATE B)
// however...the stream appears to be at eof...and accordingly c is
unaffected by get():
cout << "after putback " << is . tellg( ) << " with " << is . gcount(
) << " and next byte will be [";
cout << (c = is.get( )) << "]\n";
}
// code ends
with embedded comments where I tried to explain what kind of problems I met.
Meanwhile I saw that it is known the lacking of inheritance of "failure"
from "system_error": is it planned to fix this?
Please note that NO PROBLEM OCCURS when the "istringstream" class is
used instead of "ifstream" (I mean: the badbit is set and the exception
is thrown and caught). I already knew this, but nevertheless why should
the "ifstream" class clearly put the stream at eof WITHOUT MAKING THE
PROGRAM AWARE of that in any way?
Thanks again. Hoping for an answer...or a fix of my code.
G. Servizi