Connecting Tech Pros Worldwide Help | Site Map

reading a line from a file

  #1  
Old July 19th, 2005, 08:12 PM
Marc Schellens
Guest
 
Posts: n/a
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();
}

  #2  
Old July 19th, 2005, 08:12 PM
Victor Bazarov
Guest
 
Posts: n/a

re: reading a line from a file


"Marc Schellens" <m_schellens@hotmail.com> wrote...[color=blue]
> 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?[/color]

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.
[color=blue]
> 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();
> }
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help on reading line from file into list bahoo answers 8 April 4th, 2007 11:55 AM
Stupid C Question: fscanf to read a whole line from file thesafetylemur answers 1 May 13th, 2006 09:58 PM
reading line by line from file plmanikandan@gmail.com answers 20 March 31st, 2006 06:06 PM