reading a line from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Schellens

    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(istrea m& is)
    {
    static stringstream ioss;
    ioss.str("");

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

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

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

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

    return ioss.str();
    }

  • Victor Bazarov

    #2
    Re: reading a line from a file

    "Marc Schellens" <m_schellens@ho tmail.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(istrea m& is)
    > {
    > static stringstream ioss;
    > ioss.str("");
    >
    > is.get( *ioss.rdbuf());
    >
    > if ( (is.rdstate() & ifstream::failb it ) != 0 )
    > cout << "failbit\n" ;
    > if ( (is.rdstate() & ifstream::badbi t ) != 0 )
    > cout << "badbit\n";
    > if ( (is.rdstate() & ifstream::eofbi t ) != 0 )
    > cout << "eofbit\n";
    >
    > if( !is.eof()) is.get(); // remove delimiter
    >
    > cout << "Read line: " << ioss.str();
    >
    > return ioss.str();
    > }
    >[/color]


    Comment

    Working...