How EOF is defined for binary and ascii files

后端 未结 4 1330
星月不相逢
星月不相逢 2021-01-06 09:17

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

4条回答
  •  太阳男子
    2021-01-06 09:21

    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.

提交回复
热议问题