Thread: Terminating input on EOF

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Terminating input on EOF

    I wish to read input from standard input until EOF is detected:

    Code:
    #include <stdio.h>
    
    #define MAX     127
    
    int main()
    {
            char str[MAX+1];
            int c, i = 0;
            while (i < MAX && (c=getchar()) != EOF)
                    str[i++]=c;
            str[i] = '\0';
            printf("EOF detected\n");
            return 0;
    }
    Specifically, I want the input to immediately terminate on EOF, instead of the following:

    testing^D
    testing^D
    EOF detected
    It should be:
    testingEOF detected
    instead

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The thing is, input does terminate on EOF. You read my earlier post on this issue:
    Quote Originally Posted by whiteflags View Post
    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
    The thing is even if getchar doesn't use fread (that is by no means required) one expects to process all possible data. This means that input functions can't fail on the spot. You have to try to read the stream again, and then fail. The timeliness of EOF is important. The newline also complicates things for the user. It's more friendly, if possible, to use a sentinel value like "quit" rather than EOF. If it isn't possible then you could always use an actual file.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    when i run this program, i get the output as
    c is a good language
    ^Z
    EOF tested
    what do you want the output to be.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That is not similar input Ben, but what you showed is what always seems to work.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    7
    I want to input to end immediately on EOF.
    Meaning when I press Ctrl-D after input, it immediately terminates and prints EOF detected. Try pressing Ctrl-D immediately after typing your input.
    It gives me the following:

    c is a good language^D
    why didnt input terminate^D
    <Ctrl-D pressed here>EOF tested

    Someone asked the same question here:
    http://cboard.cprogramming.com/showthread.php?t=112982
    Last edited by vyn; 03-12-2009 at 01:47 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    EOF has to be the very first thing read or it's not EOF. I don't know how to make this any more clear, but this behavior is by design. Just use a file or something.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    stdio is buffered stream - it is not processed till the Enter is pressed. Before you press enter - you still can delete characters placed in the buffer, after you press enter - program receives a buffer and can process it, if you somehow put EOF in the stream - new line character will be there as well, and it will be echoed to the used by the comandline processor - before the line is sent to the application...

    If you want to change this behavior - you need to use non-standard methods... But better change the requirements to your program so it will be standard compliant
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    7
    Quote Originally Posted by vart View Post
    stdio is buffered stream - it is not processed till the Enter is pressed. Before you press enter - you still can delete characters placed in the buffer, after you press enter - program receives a buffer and can process it, if you somehow put EOF in the stream - new line character will be there as well, and it will be echoed to the used by the comandline processor - before the line is sent to the application...

    If you want to change this behavior - you need to use non-standard methods... But better change the requirements to your program so it will be standard compliant

    Thanks, this explained it well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  3. Never find EOF after improperly formatted input
    By MALDATA in forum C Programming
    Replies: 5
    Last Post: 09-30-2008, 01:24 PM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. Replies: 2
    Last Post: 03-12-2002, 02:32 PM