Question about 'gets' and EOF

This is a discussion on Question about 'gets' and EOF within the C Programming forums, part of the General Programming Boards category; I've never been sure about how EOF works on a system. Take for instance this example program, which counts strings ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Question about 'gets' and EOF

    I've never been sure about how EOF works on a system. Take for instance this example program, which counts strings entered until it encounters the word "quit" or the EOF.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 81
    #define LIM 100
    #define STOP "quit"
    
    int main(void)
    {
      char input[LIM][SIZE];
      int ct = 0;
    
      printf("Enter up to %d lines (type quit to quit):\n", LIM);
    
      while (ct < LIM && gets(input[ct]) != NULL && strcmp(input[ct], STOP) != 0)
        ct++;
    
      printf("%d strings entered\n", ct);
      return 0;
    }
    Then the following expected behavior is given:

    1
    2
    3
    4
    5
    ^Z
    5 strings entered
    But what is happening here?
    1
    2
    3
    4
    5^Z
    ^Z
    ^Z
    5 strings entered
    And here?
    1
    2
    3
    4
    5^Z
    6
    7
    8
    9
    ^Z
    8 strings entered

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Is this another one of those trick questions at the end of Apocalypse Now where Brando snarls, "I don't seen any EOF at all"?

    Because if it is it ain't funny, really.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    The gets function, like fgets, is usually written as a looped call to fread. Your question is answered in the manual:

    The functions fread() and fwrite() advance the file position indicator
    for the stream by the number of bytes read or written. They return the
    number of objects read or written. If an error occurs, or the end-of-
    file is reached, the return value is a short object count (or zero).
    So if you send EOF immediately after some data, it is likely that fread will return "a short object count" instead of zero like you expected, which ultimately determines gets return value. It is no different from what happens when reading a file. You may encounter EOF while you read the last line, but only the call after that fails for certain. This is so the last of the data has an opportunity to be processed.

    And remember that when you send EOF from the console there is actually two pieces of data to read: the EOF, and a newline. If the newline weren't there, input would fail like you expect.
    Last edited by whiteflags; 03-05-2009 at 10:45 PM.

  4. #4
    vyn
    vyn is offline
    Registered User
    Join Date
    Nov 2008
    Posts
    7
    1
    2
    3
    4
    5^Z
    6
    7
    8
    9
    ^Z
    8 strings entered
    I have the same question: how do I prevent the above from happening? I want the input to terminate on pressing Ctrl-D/Z. The behaviour should be like the following:

    1
    2
    3
    4
    5^Z
    5 strings entered
    The Unix cat program seems to be able to do it. How do I do the same thing in C?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    vyn, start a new thread and post your current code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21