When I use file functions in PHP, I check for EOF. I wonder if EOF actually exist in a file. When I create an empty text file, it displays 0KB. How does EOF exist in a file with 0KB?
3 Answers
There is an end-of-file control character (in the ASCII character set it's CTRL+Z or 26 or 0x1A), but it hasn't actually been needed to mark the end of a file since OSes released in the 80's. All modern OSes store the file size as metadata in the directory structure (exact format depends on filesystem) and high level file access functions will check the file size to decide when to indicate EOF to you, the programmer.
If there is an end-of-file in the data AND you have text-mode translations turned on (in most languages this is the same setting that controls NL <-> CRLF conversions), then the file access may stop when it hits that EOF character. In binary mode, reads will keep going until the file size is hit.
-
2Right, and other OSes used file separator (28) and so forth. Just another reason we should be glad using a character to mark end of file is obsolete. Mar 9, 2010 at 0:36
-
1^D is not EOF on unix. Rather, terminal devices on unix allow you to define a control character that generates an EOF condition on the device when pressed, and the default is ^D. But this does not mean the ascii byte for ^D ever appears in the stream of data from the terminal... Sep 10, 2011 at 17:44
-
In modern filesystems there is no actual EOF character on the disk. It's implemented in some file reading functions by returning a 4 byte value instead of a 1 byte value, but most functions don't use the concept.