How EOF is defined for binary and ascii files

后端 未结 4 1326
星月不相逢
星月不相逢 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.

    0 讨论(0)
  • 2021-01-06 09:23

    Found a terrific article that answers all the question! https://latedev.wordpress.com/2012/12/04/all-about-eof/

    0 讨论(0)
  • 2021-01-06 09:26

    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.

    0 讨论(0)
  • 2021-01-06 09:37

    EOF in text files is usually character 0x1A or ASCII 26 if you will.

    0 讨论(0)
提交回复
热议问题