EOF
is not stored in files. So there's no point comparing a byte from the file with EOF
. If you use mmap
, as opposed to getchar
or equivalent, then you need to stat
the file to find out how big it is.
Note that getc
, fgetc
and getchar
return an int
. Quoting the manpage (or the Posix standard), these functions return the next byte "as an unsigned char cast to an int, or EOF
on end of file or error." The value of EOF
must be such that it cannot be confused with "an unsigned char cast to an int"; typically, it is -1. It is possible for a random (signed) char
to be equal to -1
, so your test data[i]!=EOF
may eventually turn out to be true as you scan through uninitialized memory, if you don't segfault before you hit the random byte.
In Unix, text files are not necessarily terminated with NULs either. In short, you should only try to reference bytes you know to be inside the file, based on the file's size.