Thread: getchar ignores EOF

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    getchar ignores EOF

    Hi
    I've built a project,but one part of it isn't working the way I expect it to be:
    Code:
    for (i = 0; i < N; i++)
        {
            buffer[i] = getchar();
            if(buffer[i] == EOF)
            {    
                fprintf(stderr,"input error!\n");
                free(buffer);
                return;
            }
        }
    assuming that all variables are declared/defined (argument N is the size of an array I want to fill,and is passed from main)
    the problem is,let N be 3 (the value doesn't really matter),if there's nothing on input,it prints error,finish program-OK, if there's only 1 char,it does the same-OK,but if there are 2 chars (though the array is still not full) it doesn't print anything,but finish.I think it should work as well because next char should be EOF,isn't it? I use input from a text file,which should not put an \0 at the end of a string, so I really don't understand where the mistake is
    - the loop is a part of an external void function called from main
    I'll be thankful for any help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well if buffer is an array of char, then you lose information in the assignment, then try to 'restore' that information in the comparison.

    EOF CANNOT be stored in a character, so you need to store the result of getchar() in an int, if you want to successfully compare it with EOF.

    Code:
    int ch = getchar();
    if ( ch == EOF ) {
    } else {
      buffer[i] = (char)ch;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    well, I forgot to tell you that actually I stored the result in an int (it's a school project and they told us EOF might confuse if only 8bits instead of 32 is used)...so is there any other solution,why it is still not working? edit: my fault data type was int not char

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by inliner
    I forgot to tell you that actually I stored the result in an int
    So buffer is an array of ints? Or did you forget something else?

    You might want to post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    yes,exactly,buffer is an array of ints
    this should demonstrate the problem:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int N = atoi(argv[1]);
        int *buffer = malloc(N*sizeof(int));
        
        for (int i = 0;i < N; i++)
        {
            buffer[i] = getchar();
            if (buffer[i] == EOF )
            {
                fprintf(stderr,"input error\n");
                free(buffer);
                return 1;
            }
            
        }
    free(buffer);
    return 0;
    }
    so like I said, if array meets EOF at (N-1) position,it doesn't print anything,but it should..
    - I'm using linux mint, programming in gedit,compiling with gcc

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but I cannot duplicate your problem. Manually triggering EOF on the terminal works for me, and using input redirection with a file of length less than what is provided to the program at the command line also works for me, i.e., "input error\n" is printed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I think OP is talking about input:
    ab # follow by EOF. does not print 'input error'

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    bayint Naung: I quite don't understand,how did you mean it? if have only 'ab' in a file,I guess those are only two chars,is there anything more than these??? if not,how should I rewrite my program to make it work the way I want?

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I would bet that you text editor put newline in file.
    When you see N-1 char in file, there's newline char also. total N chars in file....

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    yeah,exactly,I didn't understand,actually my friends too,today I asked my lecturer and he showed me this as well (gedit put a newline in file),so you were right guys,thanks a lot,what's more,as the lecturer said,it wasn't even mistake it was an expected behaviour and was correct,though thx again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while ((c = getchar()) != EOF), AND cntrl z
    By Roger in forum C Programming
    Replies: 8
    Last Post: 10-21-2009, 09:25 PM
  2. Getchar terminating with new line or EOF
    By rocksteady in forum C Programming
    Replies: 1
    Last Post: 10-19-2007, 01:19 PM
  3. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  4. questions about getchar() and EOF
    By kermit in forum C Programming
    Replies: 4
    Last Post: 02-04-2003, 05:14 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM