strange issue reading /dev/null

Daniel Eischen deischen at freebsd.org
Thu Aug 7 16:21:11 UTC 2008


On Thu, 7 Aug 2008, Gabor Kovesdan wrote:

> Hello,
>
> I'm wondering why fgetc() returns 0xff if called with /dev/null:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int
> main(void)
> {
>       int      c;
>       FILE    *f;
>
>       f = fopen("/dev/null", "r");
>
>       if (c != EOF)
>               printf("%c\n", fgetc(f));
> }

Hmmm, are you *sure* your code should not be written as follows:

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
 	FILE *f;
 	int c;

 	f = fopen("/dev/null", "r");
 	if (f != NULL) {
 		c = fgetc(f);
 		if (c != EOF)
 			printf("%c\n", c);
 		else
 			printf("EOF encountered\n");
 	}
 	return (0);
}

-- 
DE


More information about the freebsd-hackers mailing list