Question about feof()

Joseph S. D. Yao jsdy at hadron.UUCP
Tue Dec 30 22:00:36 AEST 1986


In article <31800001 at garrity> garrity at garrity.applicon.UUCP writes:
>   The following routine ... prints a 0, followed by a 1.
>	#include <stdio.h>
>	main()
>	{ FILE	*f; char	string[16];
>		f = fopen("/dev/null","r");
>		printf("%d\n",fread(string,16,1,f));
>		printf("%d\n",feof(f));
>	}
>   If I change the "r" to a "w", ... feof() now returns a 0.

Well, the fread() on a FILE opened for writing should return an
error, and in fact returns(0).  The feof() tests for the EOF bit
in the flags, which is never set in this circumstance -- since
you can write to /dev/null (or, theoretically, any other file)
and never reach bottom.

/* STAMP OUT FASCIST PROGRAMS	*/
/* THAT ENFORCE MINIMUM AMOUNT	*/
/* OF RETURN MATERIAL!		*/
-- 

	Joe Yao		hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}
			jsdy at hadron.COM (not yet domainised)



More information about the Comp.lang.c mailing list