Mar 29, 2009 at 5:25am UTC
Hey guys, I have a tricky problem about getlien() function. I tried to use getline() to reading a whole line from a .txt file, but the returned string is only half of the original string.
Original string is: Townsville| eggs| 2.5| tomato| 4| chicken| 5 (P.S. the "|" is the delimiter)
My program is like:
string location;
string rest;
getline(fin, location, '|');
cout << location <<endl;
getline(fin, rest);
cout << rest <<endl;
...
...
It's supposed to be displayed like:
Townsville
cheese fries| 2.5| tomato sauce| 4| chicken wings| 5
But now, I get something like:
Townsville
cheese fries| 2.5| tomato sauce
The other half part of string is missing.
I can't figure out the way to solve the problem, hope u guys can help me.
Thanks a lot.
Mar 29, 2009 at 5:43am UTC
Is there a new line where it gets cut off? Otherwise I'm stumped.
Mar 29, 2009 at 7:39am UTC
No,there's no new line character, this is also my first time to have this kind of problem...
Mar 29, 2009 at 1:34pm UTC
try
getline(fin, rest, '\n');
Mar 31, 2009 at 9:50am UTC
The getline function can be used to read multiline strings by supplying it with a third argument, as xivdeusxiv suggested.