getline problem

K

Kay

I have tried to use following code to read data form a txt file. However
, I have found that it would obtain last sentence one more time. How can
I solve this problem ?

void load(istream& is) {

string temp_name, temp_cuisine, temp_mode;

while( is != NULL ){

getline(is, temp_name, ';');
getline(is, temp_cuisine, ';');
getline(is, temp_mode, '\n');

cout << temp_name;
cout << temp_cuisine;
cout << temp_mode << endl;

}

THE code display :

C:\DJGPP\myFiles\sc>a
John Jordan 6 8
Peter 5 9
Kay 2 6
Loen 8 0
Loen 8 0 <-- display one more

C:\DJGPP\myFiles\sc>
 
S

Sharad Kala

Always check the FAQ before posting -
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
I have tried to use following code to read data form a txt file. However
, I have found that it would obtain last sentence one more time. How can
I solve this problem ?

void load(istream& is) {

string temp_name, temp_cuisine, temp_mode;

while( is != NULL ){
while ( is >> temp_name >> temp_cuisine >> temp_mode) {
getline(is, temp_name, ';');
getline(is, temp_cuisine, ';');
getline(is, temp_mode, '\n');

-Sharad
 
K

Kay

I have tried to sth like this

char a;
while( is >> a ) {....}
each sentences are omitted the first character. eg

C:\DJGPP\myFiles\sc>a
ohn Jordan 6 8 <-- John
eter 5 9 <-- Peter
ay 2 6 <-- Kay
oen 8 0 <-- Leon
 
J

Jon Bell

void load(istream& is) {

string temp_name, temp_cuisine, temp_mode;

while( is != NULL ){

getline(is, temp_name, ';');
getline(is, temp_cuisine, ';');
getline(is, temp_mode, '\n');

cout << temp_name;
cout << temp_cuisine;
cout << temp_mode << endl;

}

In both C and C++, input does not trigger an end-of-file condition until
*after* you have *tried* to read past the physical end of file and
*failed*. Simply reading the last item from the file doesn't trigger an
end-of-file condition. Therefore you must always check for end-of-file
*after* you have (tried to) read something. In your example, the standard
C++ idiom for this looks like this:

while( getline(is, temp_name, ';')
&& getline(is, temp_cuisine, ';')
&& getline(is, temp_mode, '\n') ){

cout << temp_name;
cout << temp_cuisine;
cout << temp_mode << endl;

}

This works because all C++ input expressions evaluate as true/false in a
boolean context, depending on whether the input operation succeeded or
not. This also works with the stream extraction operator, for example:

while (is >> x)
{
// do something with x
}

If you're in a situation where input can fail without reaching the end of
file (e.g. when reading numeric data), follow the loop with a test for
eof():

if (is.eof())
{
cout << "End of file reached." << endl;
}
else
{
cout << "Input error." << endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top