I\'m programming C on Windows(system language is Japanese), and I have a problem about EOF of binary and ascii files.
I asked this question last week, a kind guy help
The convention of ending a file with Ctrl-Z originated with CP/M, a very old operating system for 8080/Z80 microcomputers. Its file system did not keep track of file sizes down to the byte level, only to the 128-byte sector level, so there needed to be another way to mark the end-of-file.
Microsoft's DOS was made to be as compatible with CP/M as possible, so it kept the convention when reading text files. By this time the file size was kept by the file system so it wasn't strictly necessary, just retained for backward compatibility.
This convention has persisted to the present day in the C and C++ libraries for Windows; when you open a file in text mode, every character is checked for Ctrl-Z and the end-of-file flag is set if it's detected. You're seeing the effects of backwards compatibility taken to an extreme, back to systems that are almost 40 years old.
Found a terrific article that answers all the question! https://latedev.wordpress.com/2012/12/04/all-about-eof/
This is a Windows-specific trick for text files: SUB
character, which is represented by Ctrl+Z sequence, is interpreted as EOF
by fgetc
. You do not have to have 1A
in your text file in order to get an EOF
back from fgetc
, though: once you reach the actual end of file, EOF
would be returned.
The standard does not define 1A
as the char
value to represent an EOF
. The constant for EOF
is of type int
, with a negative value outside the range of unsigned char
. In fact, the reason why fgetc
returns an int
, not char
, is to let it return a special value for EOF
.
EOF in text files is usually character 0x1A or ASCII 26 if you will.