Differences between Linux and Windows carriage return line breaks

Source: Internet
Author: User
Tags ftp client

 

Http://www.cnblogs.com/dartagnan/archive/2010/12/14/2003499.html Linux and Windows carriage return line break

Source:

"Carriage Return" vs "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 "<line feed> <press enter> ", that is, "\ n \ r". 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.
C language programming (Windows)
\ R means return to the beginning of the row. This overwrites the previous output of this row.
For example:
Int main (){
Cout <"HAHAHA" <"\ r" <"Xixi ";

}
At last, only Xixi is displayed, and hahaha is overwritten.
\ N is the carriage return + line feed. The cursor is first moved to the beginning of the line and then switched to the next line, that is, to pull the first line of the next line.
Int main (){
Cout <"HAHAHA" <"\ n" <"Xixi ";

}




The so-called carriage return and line feed control operators are inherited from the control commands of the former telex typewriter. Press enter to reset the print header, and line feed to take the paper. The difference between DOS/windows and Unix/Linux on carriage return and line feed lies in DOS/Windows's belief that 0d = 0d0a = 0a, unix/Linux adopts the working method of the telex typewriter (which is actually correct ).

In Linux, The newline is "0d" and "0d0a" in windows. We can test it through the following program:

The above red font is estimated to be the original author's mistake. The Linux line break is \ n, and the ASCII code is 0a, not 0d. For testing, you can use the OD-T X1 file name.

In Linux, VI of \ r displays blue ^ m, which is output by Ctrl + M + V. It can be used in command mode.

: % S/^ m/g, or remove it using dos2unix.

# Define max_length 15536
# Include
# Include

Using namespace STD;

String delenter (const string SRC) // filter out the carriage return line break in the string
{
String des;
For (INT I = 0; I <SRC. Length (); I ++)
{
Char tempchar = SRC [I];
If (tempchar! = 10 & tempchar! = 13)
Des. append (1, tempchar );
}

Return des;
}

Int main ()
{
Char HTML [max_length] = "";
File * fp = fopen ("linux.txt", "rb"); // file * fp = fopen ("windows.txt", "rb ");
Char Buf [16384];
While (fgets (BUF, 16384, FP ))
Strcat (HTML, Buf );
Strcat (HTML, "\ 0 ");
String S (HTML );
Cout <"string is:" <S <Endl ;;
Cout <"the size of string is:" <S. Length () <Endl;
Cout <"after del string is:" <delenter (s) <Endl;
Cout <"the size of string is:" <delenter (s). Length () <Endl;

Fclose (FP );

Return 0;
}

In the program, the linux.txt file is copied from the Linux system.
We can observe through this program that by filtering out the carriage return line break, the number of characters in the Linux File is equal to the number of lines, while

Windows is equal to twice the number of rows. But there is a problem to note:
File * fp = fopen ("linux.txt", "rb ");
Cannot be written:
File * fp = fopen ("linux.txt", "R ");
The default file Opening Method of the latter is text. If the system automatically converts the text, the above conclusion cannot be obtained.

However, you can use the hexworkshop tool in windows and use the hexdump command in Linux to directly observe the specific binary code.

Address: http://noding.bokee.com/3867119.html

History of carriage return and line feed.

Carriage Return and line feed

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 "<line feed> <return to car> ", that is, "\ n \ r". 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 become a line. If a file in Windows is opened in UNIX/MAC, A ^ m symbol may be added at the end of each line.

DOS and Windows use carriage return + line feed Cr/LF to represent the next line,

In Unix/Linux, the line break lf indicates the next line,

The Mac OS system uses the carriage return (CR) to indicate the next line.


Cr is represented by '\ R'. the ASCII code in decimal format is 13 and the hexadecimal code is 0x0d;

Lf uses the '\ n' symbol. the ASCII code is 10 and the hexadecimal value is 0x0a. therefore, on Windows, line breaks are represented by 0d 0a in text files, while on UNIX and Apple platforms, line breaks are represented by 0a or 0d in one byte.


Generally, the Runtime library on the operating system automatically determines the line feed format of text files. for example, if a program runs on Windows, a text file in CR/LF line feed format is generated, and a text file in LF format is generated when it runs on Linux. using another line break file on one platform may cause unexpected problems, especially when editing program code. sometimes the code is displayed normally in the editor, but an error occurs due to line breaks during editing. many text/code Editors Use the line break Conversion Function to swap line breaks in text files in different formats.

When FTP software is used to transfer files between different platforms, some FTP client programs automatically convert the line feed format in ASCII text transmission mode. the number of bytes transmitted may change. if you do not want to modify the original file via FTP, you can use bin mode (binary mode) to transfer text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.