Problem with fgets reading last line over and over

Discussion in 'C++' started by Trond Valen, Dec 6, 2005.

  1. Trond Valen

    Trond Valen Guest

    Hi!

    Anyone who knows what the reason might be? fgets is supposed to return
    null when reading EOF. I have a test for this, so my while loop should
    exit, but it doesn't ...

    Trond
     
    Trond Valen, Dec 6, 2005
    #1
    1. Advertisements

  2. Trond Valen wrote:
    >
    > Hi!
    >
    > Anyone who knows what the reason might be? fgets is supposed to return
    > null when reading EOF. I have a test for this, so my while loop should
    > exit, but it doesn't ...
    >


    Rule #1: Post Code

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Dec 6, 2005
    #2
    1. Advertisements

  3. Trond Valen <> writes:

    > Hi!
    >
    > Anyone who knows what the reason might be? fgets is supposed to return
    > null when reading EOF. I have a test for this, so my while loop should
    > exit, but it doesn't ...


    You forgot to post any code illustrating the observed behavior, so it
    is very difficult to know what the reason might be.

    The following should work:

    #include <stdio.h>

    int main()
    {
    char buf[42];
    char* ptr;

    while ((ptr = fgets(buf, sizeof buf, stdin)) != NULL) {
    /* do something with buf here */
    }
    return 0;
    }

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 6, 2005
    #3
  4. Trond Valen

    mlimber Guest

    Trond Valen wrote:
    > Hi!
    >
    > Anyone who knows what the reason might be? fgets is supposed to return
    > null when reading EOF. I have a test for this, so my while loop should
    > exit, but it doesn't ...
    >
    > Trond


    Rule #2: Prefer iostreams to C-style file streams:

    http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.1

    Cheers! --M
     
    mlimber, Dec 6, 2005
    #4
  5. Trond Valen

    Guest

    Trond Valen wrote:
    > Hi!
    >
    > Anyone who knows what the reason might be? fgets is supposed to return
    > null when reading EOF. I have a test for this, so my while loop should
    > exit, but it doesn't ...
    >
    > Trond


    Rule #3: Make sure you test eof correctly with your C++ streams

    http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

    Gavin Deane
     
    , Dec 6, 2005
    #5
  6. writes:

    > Trond Valen wrote:
    > > Hi!
    > >
    > > Anyone who knows what the reason might be? fgets is supposed to return
    > > null when reading EOF. I have a test for this, so my while loop should
    > > exit, but it doesn't ...
    > >
    > > Trond

    >
    > Rule #3: Make sure you test eof correctly with your C++ streams
    >

    Rule #4: If you for some reason use C style I/O, make sure you test
    eof correctly with your FILE objects.

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 7, 2005
    #6
    1. Advertisements

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. DJP
    Replies:
    7
    Views:
    8,033
    glen herrmannsfeldt
    Oct 21, 2004
  2. Replies:
    6
    Views:
    1,455
    Mark McIntyre
    Jun 25, 2003
  3. Charles Erwin
    Replies:
    4
    Views:
    753
    Joe Wright
    Oct 6, 2003
  4. DJP
    Replies:
    16
    Views:
    1,208
    Villy Kruse
    Oct 21, 2004
  5. Andy
    Replies:
    3
    Views:
    3,912
  6. Francis Moreau
    Replies:
    17
    Views:
    731
    Tim Rentsch
    Jan 4, 2011
  7. bernd
    Replies:
    5
    Views:
    229
    Mumia W.
    Jun 19, 2006
  8. John Reye
    Replies:
    43
    Views:
    1,643
    Malcolm McLean
    Nov 14, 2013
Loading...