EOF considered harmful

Tom Stockfisch tps at chem.ucsd.edu
Tue Oct 24 10:50:07 AEST 1989


In article <266 at m1.cs.man.ac.uk> ian at r6.UUCP (Ian Cottam) writes:
>	char ch;
>	while ( ! feof(stdin) ) {
>		ch= getchar();
>		putchar(ch);
>	}
>1) This program runs as quickly as the ``((ch= getchar()) != EOF)''
>   version (on my SUN3 with gcc).

The problem with this version is that feof() does
not test for an error condition on stdin, whereas
getc() will return EOF on i/o error.  So your
program might loop infinitely if there is
an i/o error.

The above code fragment will also fill up the disk
with (char)EOF if chars are unsigned.
-- 

|| Tom Stockfisch, UCSD Chemistry	tps at chem.ucsd.edu



More information about the Comp.lang.c mailing list