我想知道两者之间的区别(如果可能的话,请举例说明) CR LF (Windows)、LF (Unix)和CR (Macintosh)三种换行类型。


当前回答

“记录分隔器”或“行终止器”的可悲状态是计算黑暗时代的遗产。

现在,我们理所当然地认为我们想要表示的任何东西在某种程度上都是结构化的数据,并且符合定义行、文件、协议、消息、标记等的各种抽象。

But once upon a time this wasn't exactly true. Applications built-in control characters and device-specific processing. The brain-dead systems that required both CR and LF simply had no abstraction for record separators or line terminators. The CR was necessary in order to get the teletype or video display to return to column one and the LF (today, NL, same code) was necessary to get it to advance to the next line. I guess the idea of doing something other than dumping the raw data to the device was too complex.

Unix和Mac实际上为行尾指定了一个抽象,想象一下。遗憾的是,他们指定的是不同的。(Unix,咳咳,是先出现的。)当然,他们使用的控制代码已经“接近”于S.O.P.

由于今天我们几乎所有的操作软件都是Unix、Mac或MS操作SW的后代,我们陷入了行尾的混乱。

其他回答

它实际上只是关于文件中存储哪些字节。CR是一个字节码,用于回车(从打字机时代开始),LF类似地用于换行。它只是引用作为行末标记的字节。

维基百科上总是有更多的信息。

CR和LF为控制字符,分别编码为0x0D(十进制13)和0x0A(十进制10)。

它们用于标记文本文件中的换行符。正如您所指出的,Windows使用两个字符CR LF序列;Unix只使用LF,旧的MacOS(前osx MacIntosh)使用CR。

杜撰的历史观点:

As indicated by Peter, CR = Carriage Return and LF = Line Feed, two expressions have their roots in the old typewriters / TTY. LF moved the paper up (but kept the horizontal position identical) and CR brought back the "carriage" so that the next character typed would be at the leftmost position on the paper (but on the same line). CR+LF was doing both, i.e. preparing to type a new line. As time went by the physical semantics of the codes were not applicable, and as memory and floppy disk space were at a premium, some OS designers decided to only use one of the characters, they just didn't communicate very well with one another ;-)

大多数现代文本编辑器和面向文本的应用程序提供选项/设置等,允许自动检测文件的行尾约定并相应地显示它。

这是我发现的一个很好的总结:

回车符(CR)字符(0x0D, \r)将光标移动到行首,而不移动到下一行。此字符在Commodore和早期Macintosh操作系统(OS-9及更早版本)中用作新行字符。

换行(LF)字符(0x0A, \n)将光标向下移动到下一行,而不返回到行首。此字符在基于UNIX的系统(Linux、Mac OSX等)中用作新行字符。

行结束(EOL)序列(0x0D 0x0A, \r\n)实际上是两个ASCII字符,是CR和LF字符的组合。它将光标向下移动到下一行和该行的开头。该字符在大多数其他非unix操作系统(包括Microsoft Windows、Symbian OS和其他操作系统)中用作新行字符。

“记录分隔器”或“行终止器”的可悲状态是计算黑暗时代的遗产。

现在,我们理所当然地认为我们想要表示的任何东西在某种程度上都是结构化的数据,并且符合定义行、文件、协议、消息、标记等的各种抽象。

But once upon a time this wasn't exactly true. Applications built-in control characters and device-specific processing. The brain-dead systems that required both CR and LF simply had no abstraction for record separators or line terminators. The CR was necessary in order to get the teletype or video display to return to column one and the LF (today, NL, same code) was necessary to get it to advance to the next line. I guess the idea of doing something other than dumping the raw data to the device was too complex.

Unix和Mac实际上为行尾指定了一个抽象,想象一下。遗憾的是,他们指定的是不同的。(Unix,咳咳,是先出现的。)当然,他们使用的控制代码已经“接近”于S.O.P.

由于今天我们几乎所有的操作软件都是Unix、Mac或MS操作SW的后代,我们陷入了行尾的混乱。

NL源自EBCDIC NL = x'15',这将在逻辑上与CRLF x'odoa ascii进行比较…当将数据从大型机物理移动到中端时,这一点变得非常明显。口头上(只有奥术人士才会使用缩略语),NL已经等同于CR或LF或CRLF