Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: "EOF"??

Reply
Thread Tools

Re: "EOF"??

 
 
James Kanze
Guest
Posts: n/a
 
      02-06-2009
On Feb 6, 6:01 pm, Daniel Pitts
<(E-Mail Removed)> wrote:
> James Kanze wrote:
> > On Feb 6, 1:36 am, Daniel Pitts
> > <(E-Mail Removed)> wrote:
> >> sofin wrote:
> >>> File:
> >>> 1 2 3 4 5 6
> >>> I want to read the data.


> >>> ifstream FP;
> >>> FP.open("file name");


> >>> int temp;


> >>> do {
> >>> FP >> temp;
> >>> cout << temp;
> >>> }while(FP->eof());


> >>> Result" 1234566


> >>> Why the last digit 6 repeats twice?


> >> while (!(FP >> temp).eof()) {
> >> std::cout << temp << " ";
> >> }


> > No.


> > Remove the final white space (the new line), and there's a
> > distinct chance that the code won't display the final '6'.


> > See my other post for the correct way to do it. But in any
> > case, don't ever use ios::eof() before an input has failed.


> Can you please explain that to me? I thought that eof flag was
> only set once an attempted read resulted in passing (not
> reaching) the end of file.


No. The eof flag is set anytime the stream sees the end of
file, even in look ahead. All it means is that the next input
is guaranteed to fail. (But if it isn't set, there's no
guarantee that the next input will succeed.)

Of course, even if it were set only if input failed because of
an end of file, you still wouldn't want to use it to control the
loop, as above---if the input were "1 2 x 4 5 6", you'd go into
an endless loop.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments