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. Advertising

  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. Advertising

  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. 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. DJP
    Replies:
    7
    Views:
    7,521
    glen herrmannsfeldt
    Oct 21, 2004
  2. Charles Erwin
    Replies:
    4
    Views:
    594
    Joe Wright
    Oct 6, 2003
  3. DJP
    Replies:
    16
    Views:
    1,007
    Villy Kruse
    Oct 21, 2004
  4. Andy
    Replies:
    3
    Views:
    3,482
  5. Francis Moreau
    Replies:
    17
    Views:
    544
    Tim Rentsch
    Jan 4, 2011
Loading...

Share This Page