0

I have a an issue where fgetc() would crash upon trying to read beyond the last character in a stream (there is no file being opened, I am redirecting a text file to stdin). In the following code, with a file that contains a single character and a newline, it does not get to the "something" at the end - it does not even return EOF, merely quits.

I am somewhat at my wits' end here, really not sure what I am doing wrong. It's probably me not knowing something critical, but I have no idea what it is.

I did try to check with feof(), but it did not even get to it.

int p;
while ( (p = fgetc(stdin)) != EOF ) {
    printf("%c", p);
}
printf("something.\n");

I tried changing the previous char p to int p, to no avail.

Sunspawn
  • 817
  • 1
  • 12
  • 27

0 Answers0