0

I use fgets inside a while loop for getting use input, if I use ctrl-d to send a EOF at the begining of line, then fgets return NULL(because it encountered EOF), and terminal print "!!!", but the problem is after that the fgets function does not wait for input, the terminal keep printing "ERROR" until loop ends. I was expecting the fgets will wait for input every loop.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{

    int argNums = 0;
    while(argNums < 20){
        char argBuf[100];
        if(fgets(argBuf, 100, stdin) != NULL){
            printf("!!!");
        }else{
            printf("ERROR ");
            //exit(1);
        }
        argNums++;
    }
    return 0;
}

This is output

1
!!!2
!!!ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR 

I want to know why this happens, thanks for helping.

12
  • It is because you have not consumed argNums. You loop until it reaches 20.
    – Rachid K.
    Oct 12, 2020 at 8:31
  • Add the fgets call to the loop condition, so you check both argNums and for errors (like EOF) as part of the loop continuing. Oct 12, 2020 at 8:32
  • You can consider using feof() and clearerr() if you want to loop again after "CTRL-D" ?
    – Rachid K.
    Oct 12, 2020 at 8:38
  • 1
    Why? Because when you have reached the end of the file then there's nothing more to read. EOF leads to a flag inside the file stream to be set, and once set all input will fail. That's how it's supposed to work, you just can't input more once you've sent the EOF from the terminal. Perhaps you should rethink your way of handling end of input (as opposed to end of file), for example by asking the user to give some specific input that marks the end (like e.g. and empty line). Oct 12, 2020 at 8:53
  • 1
    Also note that no standard C stream input function will return a value that makes it possible to differ between error or end of file. You must use the feof and ferror functions to check which it was. Oct 12, 2020 at 8:55

1 Answer 1

2

Use clearerr():

The C library function void clearerr(FILE *stream) clears the end-of-file and error indicators for the given stream.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{

    int argNums = 0;
    while(argNums < 20){
        char argBuf[100];
        if(fgets(argBuf, 100, stdin) != NULL){
            printf("!!!");
        }else{
            printf("ERROR ");
            clearerr(stdin);
            //exit(1);
        }
        argNums++;
    }
    return 0;
}
1
  • 1
    You might want to add a note for the OP to check if it's really and end-of-file condition before clearing the error. If it's really is an actual error, then clearing that status won't help much. Oct 12, 2020 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.