End of file
End of file (EOF) is the boundary at a regular file's current logical length, or an end-of-input condition reported by an input interface.
For a regular file, a successful read with a positive requested count returns zero when its starting offset is at or beyond the current end. A zero-count request can also return zero without testing that boundary. A positive short read is not itself a zero-byte EOF result. [1]
Unwritten gaps before EOF yield zero-valued bytes, not an absence of bytes. Seeking beyond EOF does not extend the file. A later write can grow it; consequently, an earlier EOF observation is not proof that no future read can obtain data. [2] [3]
For an empty pipe or FIFO, EOF requires that no write end remains open. A positive-count read of an empty buffer with a writer still present instead leads to waiting or, in nonblocking mode, EAGAIN. This is a communication-lifetime condition, not a regular-file size measurement. [4]
The C feof() function tests a stream's recorded indicator, not current file length. fgetc() returns EOF while that indicator remains set, even after growth. clearerr() clears EOF and error indicators without moving the stream position; a successful fseek() also clears EOF. Clearing the indicator does not guarantee another byte. fgetc() can also return EOF for a read error: distinguish it with ferror(). [5] [6] [7] [8]
Relations
- related
- File size, File offset, Regular file, Sparse file, Short read
Limitations
- EOF does not verify a complete backup, unchanged contents or durable storage.
- The interpretation of a zero return depends on the interface, object type and requested count.
Citations
- Linux man-pages: read(2), zero count and return values
- POSIX.1-2017: read(), unwritten ranges and end of input
- Linux man-pages: lseek(2), seeking past EOF
- Linux man-pages: pipe(7), empty buffers and writer lifetime
- POSIX.1-2017: feof(), stream end-of-file indicator
- POSIX.1-2017: fgetc(), stream state and return values
- POSIX.1-2017: clearerr(), stream state and return values
- POSIX.1-2017: fseek(), stream state and return values
Further reading
Sources & caveats
- Primary-documentation synthesis. POSIX references are the cited 2017 reproductions, not the 2024 edition.
- A six-case Linux/glibc interface demonstration accompanies the related analysis; it does not test crash durability or other C libraries.