You should end every file that you make with a new line character. Many programs do this by default, and I image the editor you are using has this option. It's just a thing, text files should end with a new line.
( You don't add \n to your file, just hit enter ).
For your edit, I'm not getting the same results as you. All of my lines have to end in junk to get the last number printed.
1 2 3
|
getline(iss, val, ',');
if ( !iss.good() )
break;
|
The getline says "read the contents of iss into val until a comma or the end of the stream is found. If you do find a comma, remove it from the stream."
So, for the last number in the stream "1,2,3,4", you don't find a comma, you read to the end of the stream. This will trigger the eof bit on the stream, which makes
good()
return false. Thus, your loop breaks even though it was successful at reading a number.
Same thing when you don't have a newline at the end of your file. You set the EOF bit, instead of stopping at the last newline.
I suppose that program could add garbage to the end of the first row, but shouldn't there be a better answer? |
You don't want your other program to output junk, but it should output a newline at the end of the file.
The quickest thing to do is to remove the
good()
clause, so you have this:
Streams can be converted to bool, in which case they check against the fail bit rather than the eof bit. Trying to extract from a stream that is at EOF does trigger the fail bit though.