PAGE2GO2 HOME | INTERNET NEWS

LeighExchange - Free Advertising Network Stock Research at Internet Speed Need Money Easy and Quick?

Re: Read file problem

 List
Subject: Re: Read file problem
Poster: ricecake@gehennom.invalid(MarcusKwok)
Date: Fri, 23 Mar 2007 20:58:30 +0000 (UTC)
Related Postings: 1 2
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

 

Page2Go2.com is not responsible for content of this message.