Friday, October 26, 2018

c++ - Output to console overlaps

The reason your last line is printed twice is because your last call to getline() failed, but you still printed pLine (even though its content is undefined).


while(pFile.good()){
getline (pFile, pLine); // What happens if this line fails.
// Like when you read **past** the end of file.
cout<}

The correct version of your code is:


while(pFile.good()){
if (getline (pFile, pLine))
{ cout< }
}

But this is usually written as:


while(getline (pFile, pLine))
{
// The loop is only entered if the read worked.
cout<}

Remember that the last successful call to getline() reads up-to but not past the end of line. That mean the next call to getline() will fail and set the EOF bit.


Also note that your output is stinging together because you are not adding a '\n' seporator between your lines. Note: the getline() reads upto the next '\n' character but this termination character is not added to the string pLine.

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...