# C++ getline problem

This topic is 3875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to load data from a text file.
//loop
//end loop

My text file looks like this 1 Paul H Lehmann 1970 The getline code is actually loading the full name Paul H Lehmann and the day of birth 1970. I was trying to put inFile.ignore ( 100, '\n' ); but it didn't help me. Anyone know how to do this without having to re write my text file to this format 1 Paul H Lehmann 1970

##### Share on other sites
Get line does just that: gets the line. A text file is text, it has no way of knowing that the date following the name is a number that is not at all related to the name.

One possible solution is to read the entire line, and use sscanf to pick out your data. Another possible solution is to use markers to split various fields, although, that makes your file less human-readable and more prone to error.

##### Share on other sites
wow its been a long time since i been on these forumns

the reason that happens is by default the getline(...) function will read the entire rest of the line up to the \n

so you however tell it to read up to a certain character...so maybe set your file up like

1 ; Paul H Lehmann ; 1970

or something similar to separate each value and when you read the name tell getline to read up until the ; character

i think it is a 3rd parameter you can pass to getline function to tell it which character to stop on....just google.com the function and find out

and dont forget to read out the first ; after the id and before the name before actually trying to getline the name

##### Share on other sites
std::getline gets the whole line, as the name suggests. You can put delimiters in (as in, it'll stop when it reads a delimiter) - so std::getline(inFile, read.Name, "nn") would return "1 Paul H Lehma", stopping at the delimiter "nn" (I can't remember if it allows strings, probably just a single character only). Unfortunately, your datafile is not set up very well. Comma Seperated Values are a normal way to do this:

1,Paul H Lemann,1970

Because that way the delimiter can be the comma. As it stands, you can't tokenise your string without contextual information - something computers suck at.

perfect thanks.
woot

##### Share on other sites
Couldn't you just have it read up to the space?
or just use

gets or another function that reads up to the white space, IIRC

I/O is not my strongest area, so take what I say with a grain of lime salt :)

##### Share on other sites
Quote:
 Original post by bluefox25Couldn't you just have it read up to the space?getline(inFile,read.id,' ') ?or just usegets or another function that reads up to the white space, IIRCI/O is not my strongest area, so take what I say with a grain of lime salt :)

Of course, you could. But then parsing the file and trying to pick out the name would result in "Paul" instead of "Paul H Lehmann" because the string contains spaces.
You could solve this by checking the next non-space character and evaluate its type, judging by that consider to take the next string into the name string or not.
This system would break down though, if you happen to change the text file and have a string type information follow the name.

##### Share on other sites

This topic is 3875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.