C++ getline problem

Started by
5 comments, last by kiome 16 years, 11 months ago
I'm trying to load data from a text file.

//loop
    inFile >> read.ID;
    getline(inFile, read.Name);
    inFile >> read.birthDate;
//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
Advertisement
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.
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
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
perfect thanks.
woot
Couldn't you just have it read up to the space?
getline(inFile,read.id,' ') ?
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 :)
Quote:Original post by bluefox25
Couldn't you just have it read up to the space?
getline(inFile,read.id,' ') ?
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 :)

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.
My Blog

This topic is closed to new replies.

Advertisement