Line break and Carriage return (reprint)

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/xiaofei2010/article/details/8458605

Windows under the point of return, the effect is: carriage return line, that is \ r \ n

A carriage return under the UNIX system is a \ n

Give the following code:

#include <iostream>using namespacestd;intMain () {cout<<"This is the first line\n"; cout<<"This is the second line\r"; cout<<"This is the third line\n"; cout<<"This is the fouth line\r"; cout<<"This is the fifth line\n"; cout<<"End" ; return 0;}

The results of the operation are as follows:

Carriage return Line (\n\r): Each time the cursor moves to the beginning of the next line at the position;

NewLine (\ r): Each time the cursor moves to the beginning of the line at the bank.

See also:

the difference between carriage return and line break

The difference between them is actually a return to the line.

First, history.

The origins and differences between the two concepts of carriage return "(carriage return) and" line feed ".

Symbolic ASCII code meaning

\ n 10 line break

\ r 13 Car return CR

Before the computer appeared, there was a thing called a telex typewriter (the concept of TTY under the teletype Model 33,linux/unix), which can be played 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.

In Windows:

' \ R ' returns to the beginning of the current line without changing to the next line, and if the output is followed, the previous contents of the bank will be overwritten;

' \ n ' to wrap to the next line in the current position without going back to the beginning of the line;

Unix system, the end of each line only "< line >", that is, "\ n", the Windows system, the end of each line is "< Enter >< line >", that is "\ r \ n", the Mac system, the end of each line is "< Enter >", that is "\ r ";。 A direct consequence of this is that if the file under the Unix/mac system is opened in Windows, all the text will be turned into one line, and if the files in Windows are opened under Unix/mac, a ^m symbol may appear at the end of each line.

Cases:

$ Echo-en ' 12\n34\r56\n\r78\r\n ' > Tmp.txt

Viewing this file in Windws and Linux, respectively: Linux encounters a newline character ("\ n") for carriage return + line wrapping, but the carriage return is only displayed as a control character ("^m") and does not take place of carriage return. In Windows, a carriage return + newline character ("\ r \ n") will enter + line, missing a control, or another line that does not have the correct order.


See also:

The legend of carriage return---and talk about \n\r difference (reprint)

If you have experience with programming under Windows, you will find that Windows knocks down the ENTER key to produce two characters CR and LF, opening a text file under Windows with the 16 binary editor will also see that line breaks are represented by 0D and 0 A, which is the ASCII encoding of CR and LF. In the Unix class system, the newline has only one character LF, so the text file in Unix opens in Notepad with a non-resolvable character and loses a newline format, with all characters Fuliancheng a line.
Because Notepad under Windows does not know if it is a newline (only the CR and LF consecutive occurrences can be interpreted as newline), then it is treated as a non-printable character, which is a black box. CR and LF represent "carriage return" (carriage return) and "line feed", ASCII encoding 13 and 10, in C with \ R and \ n.
Why is a line break in two characters under Windows? This is the story to tell today: "The Legend of Carriage return"
At the time of the birth of the computer, an ancient terminal (console terminal) Telex typewriter made by teletype company was widely used ASR33. ASR33 can play 10 characters per second. However, it has a problem, that is, when a line is wrapped, the print head moves from the end of the line to the beginning of the row and then down one line to use for 0.2 seconds, just can hit two characters. If there are new characters coming in this 0.2 seconds, then this character will be lost, then no buffer can be staged. Teletype's researchers figured out a way to solve the problem by adding two characters to the end of each line. 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.
Here are some references:
History of the Teletype Corporation
http://www.kekatos.com/teletype/
The teletype Corporation ASR teletype (1967).


(This is the kind of machine that Gates played at Lakeside Middle School in 1968, and they wrote basic programs through this terminal.) Because ASR 33 uses only uppercase letters, the basic program is primarily capitalized
Later, the computer's prehistoric era ended, Minicomputer was born, modern civilization of the keyboard was invented, but the carriage return and the concept of the line is still preserved. Some computer designers think that adding two characters to the end of each line is too wasteful and unnecessary, plus one. So there was a disagreement.
The UNIX system uses <line feed> to represent line breaks, with only one newline at the end of each line \n,msdos and the end of each line in the Windows system is < carriage return >< line feed > (<carriage return> <line feed>) \r\n,apple mac System, the end of each line is < Enter > (<carriage return>) that is \ r. A direct consequence of this is that when the files in the UNIX/MAC system are opened in Windows, all the text becomes one line, and some text editors may have a ^m symbol at the end of each line if the files in Windows are opened under Unix/mac.
I think with two characters to show that the line is a little superfluous, but in the network of the world this phenomenon is a large number of, many network protocols require that the message must use the CR-LF line-wrapping mode.
What do you think? The issue was discussed shortly before in the CU forum: http://bbs2.chinaunix.net/thread-1067432-1-1.html
Does this have anything to do with programming?
Yes, but in standard C the flow of standard C provides a system-independent abstraction layer, which is usually not realized.
You can experiment with the Windows system:

Program One:
----------------------------------------------

1#include <stdio.h>2 intMainvoid)3 {4         inti;5FILE *FP;6         if((Fp=fopen ("Test.txt","W")) ==NULL)7         {8fprintf (stderr,"Open File error\n");9                 return 1;Ten         } One      A          for(i=0;i< -; i++) -fprintf (FP,"test\n"); -          the fclose (FP); -      -         return 0; -}

Program Two:
-------------------------------------------------------------------------------------

#include <stdio.h>intMainvoid){        inti; FILE*FP; if((Fp=fopen ("Test.bin","WB")) ==NULL) {fprintf (stderr,"Open File error\n"); return 1; }             for(i=0;i< -; i++) fprintf (FP,"test\n");            Fclose (FP); return 0;}

--------------------------------------------------------------------------------------
Program One output file size is 600 bytes, program two output file size is 500 bytes, open program one with Notepad output no problem, one test per line, open program two output found all the test connected to a line, test is a black box symbol delimited. With UltraEdit-32 in 16 binary editing mode to open Test.bin can see the black box symbol is 0A is \ n, open Test.txt will find the line is \ r \ n, this is the two file size 100 bytes difference reason. Unix class System users encounter this distress when they open a file in Windows.
Why is there such a difference?
After all, it originates from UNIX systems, C uses \ n for line breaks, and the newline symbol in the actual file needs to be consistent with the operating system, so when we use fopen in C to open a text file stream implements the conversion between the actual line break and the C \ n. In Windows when we open a text file with fopen, and then read from it to \ r \ n Stream will be converted to \ n, and when we write to the file \ n stream will be converted to \ r \ n. Program one is to open a text file, program Two Open is a binary file, because the stream is only a text file for a newline representation of the conversion, in binary mode open stream will not do any processing. So when you open a text file in binary mode, you will be confused, and you must interpret \ r \ n yourself, and the same problem will occur when you open the binary file in text mode. This also explains why files in Unix class systems do not differentiate between text files and binary files.

Does this happen when we use the standard input and output functions?
Back to our familiar standard input and output stdin,stdout
C's console program is loaded into memory before the process runs the C run-time library automatically opens three devices and correlates to three streams: standard input stream stdin, standard output stream stdout, standard error stream stderr
Typically in a general-purpose computer, these three streams correspond to devices that are not redirected: keyboards, monitors, monitors. These three are all character devices, so it is opened in a text file mode, in Windows when we type enter key on the keyboard to generate the character \ r \ n, but in the OS kernel to the keyboard driver read the word character sent to the stream buffer stream will be converted to \ n, when we output to the console \ n stream convert it to \ r \ n and then to the kernel, we have to face this reality when we bypass the standard input output and call Windows Coredll.lib for console input and output, and the programmer is responsible for this transition.

Line break and Carriage return (reprint)

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.