TONT 40233 为什么文本文件要以Ctrl+Z结尾?

当世界年纪还小的时候。

原文链接:https://blogs.msdn.microsoft.com/oldnewthing/20040316-00/?p=40233

Actually, text files don’t need to end in Ctrl+Z, but the convention persists in certain circles. (Though, fortunately, those circles are awfully small nowadays.)

事实上,文本文件并不需要以Ctrl+Z结尾,但这种惯例在特定的场合下仍然保持着。(尽管很幸运的是,这种特定的场合现在已经越来越少了。)

This story requires us to go back to CP/M, the operating system that MS-DOS envisioned itself as a successor to. (Since the 8086 envisioned itself as the successor to the 8080, it was natural that the operating system for the 8086 would view itself as the successor to the primary operating system on the 8080.)

事情的起因要追溯回CP/M,那个曾经让MS-DOS展望自己作为其继承者的操作系统。(鉴于8086将其自身视为8080的继承者,面向8086开发的操作系统自然也将其视为针对8080开发的主要操作系统(CP/M)的继承者。)

In CP/M, files were stored in “sectors” of 128 bytes each. If your file was 64 byte long, it was stored in a full sector. The kicker was that the operating system tracked the size of the file as the number of sectors. So if your file was not an exact multiple of 128 bytes in size, you needed some way to specify where the “real” end-of-file was.

在CP/M里,文件存储在以128个字节为单位的『扇区』里。如果你的文件大小是64字节,那也会占用一整个扇区。问题的根源就在于系统是按照扇区数量来计算文件大小的,所以如果你的文件大小不是128字节的整数倍,就需要以某种方式来指出文件『真正的』结尾在哪里。

That’s where Ctrl+Z came in.

这里就轮到Ctrl+Z出场了。

By convention, the unused bytes at the end of the last sector were padded with Ctrl+Z characters. According to this convention, if you had a program that read from a file, it should stop when it reads a Ctrl+Z, since that meant that it was now reading the padding.

依照惯例,最后一个扇区未使用的字节将使用Ctrl+Z字符进行对齐。按照这个习惯,如果某个程序需要读取文件,那么当其读到Ctrl+Z字符时就应该停下了,意味着它已经读到了扇区的对齐边界。

To retain compatibility with CP/M, MS-DOS carried forward the Ctrl+Z convention. That way, when you transferred your files from your old CP/M machine to your new PC, they wouldn’t have garbage at the end.

为保留与CP/M的兼容性,MS-DOS也保留了以Ctrl+Z标记文件末尾的惯例。如此一来,当你将文件从你的旧CP/M机器上复制到新PC上的时候,末尾才不会出现一大堆垃圾字符。

Ctrl+Z hasn’t been needed for years; MS-DOS records file sizes in bytes rather than sectors. But the convention lingers in the “COPY” command, for example.

Ctrl+Z(对齐)已经不是必要措施很多年了。MS-DOS是按照字节数、而不是扇区数计算文件大小的。不过,这种惯例在某些场合下还是保留的,例如COPY命令。(译注:可以在命令提示符下使用copy con <文件名>的方式,在控制台下创建一个文本文件,当录入完毕后,输入一个Ctrl+Z字符,系统会以此判断用户已录入完毕,并将文件进行保存。)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

 剩余字数 ( Characters available )

注:请不要在评论中插入任何链接,否则将自动被识别为垃圾评论,博主将完全看不到。

Notice: please DO NOT add any links in your comment, otherwise it would be identified as SPAM automatically.

*