5

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 3

11

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.

3
  • 2
    Right, 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.
    – Ben Voigt
    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
  • 'hasn't actually been needed' oh? What about for pipes?
    – Benubird
    Aug 5, 2013 at 10:08
1

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.

0

it doesn't exist.. but file size does, and that's how you know where is eof

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.