Line breaks in Linux and Windows
Carriage Return and line feed generate background
The origins and differences between carriage return and line feed.
Before the computer appeared, there was a kind of device called teletype model 33, which can contain 10 characters per second. But there is a problem, that is, when a line breaks a line, it takes 0.2 seconds, just two characters. If a new character is passed in the 0.2 s, the character will be lost.
As a result, the developers thought of a way to solve this problem, that is, adding two end characters after each line. One is "enter", which tells the typewriter to position the print head on the left boundary, and the other is "line feed", which tells the typewriter to move the paper down one line.
This is the source of "line feed" and "Carriage Return". They can also be seen in their English names.
Later, computers were invented, and these two concepts were invented on computers. At that time, memory was very expensive. Some scientists thought it would be too waste to add two characters at the end of each line. Just add one character. As a result, there were differences.
In Unix systems, each line ends with "<line feed>", that is, "\ n". In Windows systems, each line ends with "<press enter> <line feed> ", that is, "\ r \ n". In MAC systems, the end of each line is "<press enter> ". One direct consequence is that if a file in UNIX/MAC is opened in windows, all the text will be changed to a line; if a file in Windows is opened in UNIX/MAC, A ^ m symbol may be added at the end of each line.
Files created in Windows End with \ n \ r, while Unix-like systems like Linux and Mac end with \ n.
Therefore, when UNIX text is sent to Windows, line breaks will be lost (which can be correctly recognized by ultraedit). In turn, the ^ m symbol will appear.
Text line breaks used in windows and other operating systems are different from those used in Unix/Linux operating systems. line breaks entered in Windows systems are not displayed as line breaks in Unix/Linux ", it is displayed as ^ m (this is a special mark specified by Linux and other systems. It occupies the size of a character, not a combination of ^ and M, which cannot be printed ). In Linux, many text editors (command lines) will add their own line breaks after displaying this mark to avoid confusion (only for display, the supplemented line breaks will not be written to files, there are dedicated commands to replace the Windows line break with the Linux line break ).
Line breaks in Unix/Linux systems are ignored in the Windows text editor, and the entire text is messy.
For Windows, the newline is \ r \ n, and The hexadecimal value is 0d0a.
Linux line feed is \ n, and The hexadecimal value is: 0a
Therefore, if you use NotePad to view files stored in Linux on Windows, there will be black spots. in Linux, we can use commands to convert the file format in Linux to win.
Unix2dos converts a Linux File Format to a Windows file format.
Dos2unix converts Windows format to Linux file format.
Delete the Windows line break ^ m in Linux
One problem found during OJ's question determination: the Code read in % C will report wa. It was later found to be related to scanf. In Linux, % C reads the \ n and \ r characters. Therefore, delete the ^ m (that is, \ r) character.
There are many deletion methods. I found a simple one.
To drop and write the MNS in a.txt to B .txt, run the following command: CAT a.txt | tr-d "^ m"> B .txt.
Note: ^ m in the statement is input through Ctrl + V, CTRL + M. Special/R character
The UNIX line breaks are only: \ r
Line breaks in DOS: \ r \ n
The specific ASCII code of \ r is: 13
The ASCII code of \ n is: 10
Win: "\ r \ n", 0x0d0a;
Linux: "\ n", 0x0a;
Mac: "\ r", 0x0d;
Carriage Return and line feed generate background
The origins and differences between carriage return and line feed.
Before the computer appeared, there was a kind of device called teletype model 33, which can contain 10 characters per second. But there is a problem, that is, when a line breaks a line, it takes 0.2 seconds, just two characters. If a new character is passed in the 0.2 s, the character will be lost.
As a result, the developers thought of a way to solve this problem, that is, adding two end characters after each line. One is "enter", which tells the typewriter to position the print head on the left boundary, and the other is "line feed", which tells the typewriter to move the paper down one line.
This is the source of "line feed" and "Carriage Return". They can also be seen in their English names.
Later, computers were invented, and these two concepts were invented on computers. At that time, memory was very expensive. Some scientists thought it would be too waste to add two characters at the end of each line. Just add one character. As a result, there were differences.
In Unix systems, each line ends with "<line feed>", that is, "\ n". In Windows systems, each line ends with "<press enter> <line feed> ", that is, "\ r \ n". In MAC systems, the end of each line is "<press enter> ". One direct consequence is that if a file in UNIX/MAC is opened in windows, all the text will be changed to a line; if a file in Windows is opened in UNIX/MAC, A ^ m symbol may be added at the end of each line.
Files created in Windows End with \ n \ r, while Unix-like systems like Linux and Mac end with \ n.
Therefore, when UNIX text is sent to Windows, line breaks will be lost (which can be correctly recognized by ultraedit). In turn, the ^ m symbol will appear.
Text line breaks used in windows and other operating systems are different from those used in Unix/Linux operating systems. line breaks entered in Windows systems are not displayed as line breaks in Unix/Linux ", it is displayed as ^ m (this is a special mark specified by Linux and other systems. It occupies the size of a character, not a combination of ^ and M, which cannot be printed ). In Linux, many text editors (command lines) will add their own line breaks after displaying this mark to avoid confusion (only for display, the supplemented line breaks will not be written to files, there are dedicated commands to replace the Windows line break with the Linux line break ).
Line breaks in Unix/Linux systems are ignored in the Windows text editor, and the entire text is messy.
For Windows, the newline is \ r \ n, and The hexadecimal value is 0d0a.
Linux line feed is \ n, and The hexadecimal value is: 0a
Therefore, if you use NotePad to view files stored in Linux on Windows, there will be black spots. in Linux, we can use commands to convert the file format in Linux to win.
Unix2dos converts a Linux File Format to a Windows file format.
Dos2unix converts Windows format to Linux file format.
Delete the Windows line break ^ m in Linux
One problem found during OJ's question determination: the Code read in % C will report wa. It was later found to be related to scanf. In Linux, % C reads the \ n and \ r characters. Therefore, delete the ^ m (that is, \ r) character.
There are many deletion methods. I found a simple one.
To drop and write the MNS in a.txt to B .txt, run the following command: CAT a.txt | tr-d "^ m"> B .txt.
Note: ^ m in the statement is input through Ctrl + V, CTRL + M. Special/R character
The UNIX line breaks are only: \ r
Line breaks in DOS: \ r \ n
The specific ASCII code of \ r is: 13
The ASCII code of \ n is: 10
Win: "\ r \ n", 0x0d0a;
Linux: "\ n", 0x0a;
Mac: "\ r", 0x0d;