peter.vanna@gmail.com wrote:
> I would like to write a program to read a text file (data.txt) on
> Windows and Linux machines.
>
> data.txt is formated as follows:
>
> 1 10 100 1000
> 2 20
> (a new but empty line)
>
>
> My C++ program is:
>
> ***************************************
> ifstream fp_input("data.txt");
> int a,b;
>
> while(!fp_input.eof())
This usually does not do what you want, since eof() is not set until
*after* a read has failed. See:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
and related questions.
[rest of code snipped]
> However, the output of my program is:
>
> 1: 10 100 1000
> 2: 20
> 2:
>
> I don't know why there is an extra "2" in the end of the output.
>
> Could someone give me a clue to modify my program such that it can
> display "data.txt" properly under Windows and Linux?
See above.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply