while(file.good()&&!file.eof()) run once more

Hi,

When I run the following code, the while loop will run more once time.

The file contents are:

A_LABEL,A_NAME
B_LABEL,B_NAME
C_LABEL,C_NAME(END OF FILE)

However, when I run the code, and cout to test, the result is something like following:

1.st: label=A_LABEL, name=A_NAME
2.nd: label=B_LABEL, name=B_NAME
3.rd: label=C_LABEL, name=C_NAME
4.th: label='',name=C_NAME

So totally while loop run 4 times. But it should be only three time.

if (file.is_open()){
while (file.good() && !file.eof())
{
getline ( file, label, ',' );//label is a string
getline ( file, name, '\n' );//name is a string

label.erase(std::remove(label.begin(), label.end(), ' '), label.end());

name.erase(std::remove(name.begin(), name.end(), ' '), name.end());

///TODO

}//end while
}//end if

Thank you.
Best
Last edited on
see answers to the duplicate http://www.cplusplus.com/forum/general/217637/
Cubbi's answer is wonderful.
Topic archived. No new replies allowed.