reading a line from a file

M

Marc Schellens

The following routine ends up always the fail bit set.
I can extract from the same stream e.g. floats without a problem
(so it cannot be the stream)
Any obivious thing I missed?
Thanks,
marc


const string ReadLine(istream& is)
{
static stringstream ioss;
ioss.str("");

is.get( *ioss.rdbuf());

if ( (is.rdstate() & ifstream::failbit ) != 0 )
cout << "failbit\n";
if ( (is.rdstate() & ifstream::badbit ) != 0 )
cout << "badbit\n";
if ( (is.rdstate() & ifstream::eofbit ) != 0 )
cout << "eofbit\n";

if( !is.eof()) is.get(); // remove delimiter

cout << "Read line: " << ioss.str();

return ioss.str();
}
 
V

Victor Bazarov

Marc Schellens said:
The following routine ends up always the fail bit set.
I can extract from the same stream e.g. floats without a problem
(so it cannot be the stream)
Any obivious thing I missed?

You missed posting the code that calls this function. What do
you have in 'is' istream? If it's empty, then an attempt to read
past the end will set the fail-bit too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top