The line endings of text files differ from one operating system to another.
1. The provisions of each system regarding the line-tailed character (End-of-line)Unix ends with ' \ n ' at the end of each line, and the Windows system ends with "\ r \ n" at the end of each line.
2. Concept differentiation
Chinese name |
English name |
English abbreviations |
English explanation |
The C language represents |
ASCII code |
Enter
|
Carriage return |
CR |
Return |
\ n |
0x0a |
Line break |
Line feed |
LF |
New Line |
\ r |
0x0d |
3. Enter and line historyBefore the computer appeared, there was a gadget called a telex typewriter (teletype Model 33) that could play 10 characters per second. But it has a problem, that is, when the line is finished, it will take 0.2 seconds to hit two characters. If there are new characters coming in this 0.2 seconds, then this character will be lost.
So, the developers think of a way to solve this problem, is to add two after each line to end the character. One is called "carriage return", which tells the typewriter to position the printhead at the left border, and the other is called "line break", telling the typewriter to move the paper down one line.
This is the origin of "line break" and "carriage return", from their English name can also be seen in one or two.
Later, the computer invented, these two concepts are also like to the computer. At that time, memory was expensive, and some scientists thought it would be too wasteful to add two characters at the end of each line. So, there was a disagreement. As a result, Unix ends with ' \ n ' at the end of each line,Windows system ends with "\ r \ n" At the end of each linemac system at the end of each line is ' \ R ',As a consequence, if the file under the Unix/mac system is opened in Windows, all the text will become a line, and if the files in Windows are opened under Unix/mac, a ^m symbol may appear at the end of each line.
4. Programming-related in the Windows System, the text file is " \ r \ n " represents line break . Write line breaks with fputs and other functions ' \ n ' , Windows will '\ n ' is implicitly converted to ' \ r \ n ' and then written to the file. read line breaks with fgets and other functions'\ n ', Windows implicitly converts the "\ r \ n" in the file to ' \ n ' and then to the variable.
5. Example Analysis Generates a text containing a newline (\ n, 0x0A) and carriage return (\ r, 0x0D) combination of $ Echo-en ' 1\n2\r\n3 ' > Temp View text in hexadecimal mode:
$ XXD Temp
View text in Linux: $ xxd-r temp $ vim Temp
View text in Windows:
6. Conversion of text files between different platforms
The editor implements the transformation. The Notepad++/ultra edit/sublime TEXT2 provides conversion capabilities. The conversion is implemented with the Linux command. Windows to Unix$ Sed-e ' s/.$//' mydos.txt > Myunix.txtUNIX to Windows$ Sed-e ' s/$/\r/' myunix.txt > Mydos.txtThe conversion is implemented with the Linux command.
$ Dos2unix filename