Is a line break a magic horse? Line breaks

Source: Internet
Author: User

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.

Google found the following information:

Line BreakThe carriage return is the start line of the line.Line Break

'\ N' 10 newline)
'\R'13 press enter (return)

In Windows, press ENTERLine BreakThe ID is "\R\ N ". But not in Linux and other systems "\R"Symbol.

When parsing the content of a text or other format file, it is often necessary to determine the place where the carriage return line is wrapped. In this case, you must determine that "\R\ N "must determine" \ n ".

 

The concept is vague, so let's write a test code!

 

Bool checklinesymbol (){
Char * filestr;
Int32 length = 0;
Int32 I = 0;
Int32 number = 0;
Filestr = loadpolicyfile ();
If (filestr = NULL ){
Return false;
}

Length = cstrlen (filestr );
Ctrace ("file char length: \ n ");
Ctrace ("% d \ n", length );

For (I = 0; I <length; I ++ ){
Number = (int32) (* (filestr + I ));
Ctrace ("number: % d \ n", number );
}

Ctrace ("test checklinesymbol is end! \ N ");

}

 

It does not matter if the Code cannot be understood. The main function of the above Code is to output the numbers of each character from a TXT file. In my text file, the main character "\ n" is written, and a carriage return operation is performed at the same time. I want to see what the result is. The output result contains four characters, which are 92,110, 13, and 10. That is to say, I only write 2 characters, but read it through the file operation function. It reads 4 characters for me. So what are the three numbers respectively.

 

I checked the assii code table. 92 is '\', 110 is 'n', and 13 and 10 are control symbols, namely 'cr 'and 'nl '. It indicates that we are editing a text file (nodepad. Each time you call a carriage return key, the nodepad Editor automatically adds two control symbols (13 and 10) to us.

 

Later, I used the vim text editor for the same editing. The output result is 92,110 and 10 characters. Note: In the vim Editor, press a enter key. The editor only adds a 10 control symbol to indicate a line break.

 

Here I will introduce a topic, that is, why should Microsoft engineers Add a \ r when the notepad editor presses the Enter key. But does vim, the Linux gang, not add this control symbol? The readers will study it on their own. I don't care about these things. When you study it, Google. Haha

 

C code
  1. Bool checklinenumber (){
  2. Char NL = '\ n ';
  3. Char RL = '\ R ';
  4. Int32 number = 0;
  5. Number = (int32) nL;
  6. Ctrace ("\ n in code number is: \ n ");
  7. Ctrace ("% d \ n", number );
  8. Number = (int32) RL;
  9. Ctrace ("\ n in code number is: \ n ");
  10. Ctrace ("% d \ n", number );
  11. Ctrace ("test checklinenumber is end ");
  12. }
bool checkLineNumber() { char nL = '\n'; char rL = '\r'; Int32 number = 0;  number = (Int32)nL; cTrace("\\n in code number is: \n"); cTrace("%d\n", number);  number = (Int32)rL; cTrace("\\n in code number is: \n"); cTrace("%d\n", number);  cTrace("test checkLineNumber is end"); }  

 

The above code mainly describes how to write the code. There is a problem called character escape characters. When parsing the data in a file, we usually need to determine whether the file needs to wrap. In this way, we will know that there is a line break, and there is no line break. Think of our future logic processing.

So why should we introduce character escape characters? I personally think it is mainly for convenience of memory. '\ R' indicates the carriage return symbol. '\ N' indicates a line feed, and a foreign language is newline. When the code is compiled, the compiler will handle it and convert '\ R' to 13.

In fact, we can write it as char NL = 13; to indicate that this character is the carriage return character. However, char NL = '\ R' is more intuitive. Here I will make a guess: in the C stage of compilation, char NL = 13; this code line may be faster than char NL = '\ R. This is because the compiler must perform some additional processing on the next code. Of course, this is not necessary, because after all, these consumption can be ignored, and the processing speed is relative to the current computer.

Now let's talk about the functions of the code above, mainly to see the values of '\ R' and' \ n' respectively. The output result is '\ R' to escape to 13,' \ n' to 10.

 

Now, analysis is complete! I have a general understanding. I want to start writing code. Oh, do you understand?

 

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.