全部博文(230)
分类:
2008-04-14 17:36:56
If you go to the various internet protocol documents, such as , , , or , you'll see that they all specify CR+LF as the line termination sequence. So the the real question is not "Why do CP/M, MS-DOS, and Win32 use CR+LF as the line terminator?" but rather "Why did other people choose to differ from these standards documents and use some other line terminator?"
Unix adopted plain LF as the line termination sequence. If you look at , you'll see that the onlcr option specifies whether a LF should be changed into CR+LF. If you get this setting wrong, you get stairstep text, where
each line beginswhere the previous line left off. So even unix, when left in raw mode, requires CR+LF to terminate lines. The implicit CR before LF is a unix invention, probably as an economy, since it saves one byte per line.
The unix ancestry of the C language carried this convention into the C language standard, which requires only "\n" (which encodes LF) to terminate lines, putting the burden on the runtime libraries to convert raw file data into logical lines.
The C language also introduced the term "newline" to express the concept of "generic line terminator". I'm told that the ASCII committee changed the name of character 0x0A to "newline" around 1996, so the confusion level has been raised even higher.
Here's another discussion of the subject, from a unix perspective.
(Uh, CR/LF means Carriage Return/Linefeed.)
The codes for this are
Hex | Decimal | Visual Basic | Delphi | C++ Builder | Java | Paradox | |
---|---|---|---|---|---|---|---|
CR | 0D | 13 | vbCR | #13 | \r | ||
LF | 0A | 10 | vbLF | #10 | |||
CR/LF | 0D0A | 13,10 | vbCRLF vbNewLine | #13#10 | \n | newLine() | \n |
Tab | 09 | 9 | vbTab | #9 | \t |
Well, there is more - the line termination character sequence is operating system dependent. The C \n, the VisualBasic vbNewLine, and the Java newLine() will be interpreted according to the following table.
Operating Environment | Sequence |
---|---|
MS DOS/MS Windows | CRLF |
Unix | LF |
Macintosh | CR |
As a result, moving "straight text" files between systems can cause pretty severe problems. (In windows, DOS Edit and WinWord convert unix line terminators to DOS line terminators, but notepad just shows the unix teminators as boxes.)