I'm trying to read user input into a buffer by using fgetc(stdin) in c. But apparently i am using it in a wrong way, for determining if the user wants to close the program. What am i doing wrong, and how can i check for ctrl-d correctly, to terminate my program.
My code looks like this:
int read_chars = 0;
while((buf[read_chars] = fgetc(stdin)) != EOF) {
//do stuff, (change size of buf, break on \n...)
read_chars++;
}
if(feof(stdin)) {
printf("eof");
//exit(EXIT_SUCCESS)
}
if(ferror(stdin)) {
printf("error");
//exit(EXIT_FAILURE), ...
}
This works fine, until i press ctrl-d. When i do so, "error" is immediately printed.