C++: ifstream::getline problem

Posted by Jay on Stack Overflow See other posts from Stack Overflow or by Jay
Published on 2011-01-16T06:04:53Z Indexed on 2011/01/16 6:53 UTC
Read the original article Hit count: 317

Filed under:
|
|
|
|

I am reading a file like this:

char string[256];

std::ifstream file( "file.txt" ); // open the level file.

if ( ! file ) // check if the file loaded fine.
{
    // error
}

while ( file.getline( string, 256, ' ' )  )
{
    // handle input
}

Just for testing purposes, my file is just one line, with a space at the end:

12345 

My code first reads the 12345 successfully. But then instead of the loop ending, it reads another string, which seems to be a return/newline.

I have saved my file both in gedit and in nano. And I have also outputted it with the Linux cat command, and there is no return on the end. So the file should be fine.

Why is my code reading a return/newline?

Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about file-io