Connecting Tech Pros Worldwide Help | Site Map

reading a line from a file

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:12 PM
Marc Schellens
Guest
 
Posts: n/a
Default reading a line from a file

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, 07:12 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.