explaining the getline function

Dec 3, 2011 at 1:00pm
When using the getline function I understand that it is supposed to stop when it see the first space.

But when you use the following code
getline(myfile,tempName, '#');

Isn't it supposed to stop when it sees the first #

The problem that I see arising is it still stops when it sees the first space again, when you have the pound sign delimiter.

So the following code
1
2
getline(myfile,tempName, '#');
      myfile >> tempName;

where tempName is a string it just displays a first name and does not display a first and second name

What if there is never a pound sign then does it go into an infinite loop?
Dec 3, 2011 at 3:41pm
Delete line 2, that's undoing whatever getline did.
Dec 3, 2011 at 3:53pm
Also, getline() stops at and discards the first newline, if not given some other explicit delimiter instead.
Dec 3, 2011 at 5:39pm
Right and the delmiter in this instance is the # sound. Also the myfile >> tempName writes the name to the file.

The question I got now is I thought the getline(myfile.tempName, '#') would read everything until the # is found. Then myfile >> tempName would then write the information to the file.

What happens if inside myFile...there is a name of John Jacob Smith # or John Jacob # or just John #. Right now it is just reading John in all 3 instances.
Dec 3, 2011 at 5:45pm
myfile >> tempName reads from the file and puts it into tempName,
myfile << tempName writes tempName to the file.

The way you can tell is that the >> and << point towards where the data is going.
Last edited on Dec 3, 2011 at 5:46pm
Dec 3, 2011 at 5:59pm
Okay I have figured it out. I think another issue I was having was that I wasn't calling temp name at the beginning. When I changed it up to where it was calling it first...all displayed correctly.

Thanks for all of the help.
Topic archived. No new replies allowed.