Re: "EOF"??

Discussion in 'C++' started by James Kanze, Feb 6, 2009.

  1. James Kanze

    James Kanze Guest

    On Feb 6, 6:01 pm, Daniel Pitts
    <> wrote:
    > James Kanze wrote:
    > > On Feb 6, 1:36 am, Daniel Pitts
    > > <> 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:
    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
    James Kanze, Feb 6, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SL_McManus

    A question about EOF

    SL_McManus, Dec 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    469
    Jim Gibson
    Dec 4, 2003
  2. Sacha Korell
    Replies:
    2
    Views:
    14,860
    Mattias Sjögren
    Sep 6, 2003
  3. Kobu
    Replies:
    10
    Views:
    610
    Keith Thompson
    Mar 4, 2005
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    679
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,032
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page