The difference between the LF and CR

First, tell us about the "Enter" (carriage return, '\ r') and "wrap" (line feed, '\ n') and the origins of the difference between these two concepts. Before the computer still does not appear, there is thing called teletype (Teletype Model 33), and can play 10 characters per second. But it has a problem, that is, when the kick line wrap, use 0.2 seconds to go, just to play two characters. If at this 0.2 seconds inside, there are new characters pass over, then the character will be lost. Thus, the development staff thought of a way to solve this problem is to add after each line indicates the end of the two characters. Called "Enter", tells the typewriter print head is positioned at the left margin; the other is called "wraps around" to tell the paper typewriter down one row. This is the "wrap" and "Enter" in origin, from their English names can be seen 12. 

    Later, the invention of computers, these two concepts will be like to on your computer. At that time, the memory is very expensive, some scientists believe that in the end of each row plus two characters too wasteful, plus a can. As a result, there have been differences:

  • Unix systems, the end of each row only "<wrap>", i.e., "\ n-";
  • Inside Windows system, the end of each line is "<ENTER> <line feed>", i.e., "\ r \ n";
  • Mac system, the end of each line is "<Enter>", that is, "\ r".

    A direct consequence is that the files under Unix / Mac system opened in Windows, then all the text will become a line; and Windows in the file opened under Unix / Mac, then at the end of each line might be more of a ^ M symbol. Some common escape characters below:

    Note that: in the Windows system the ENTER key is treated as \ combinations r \ n are used, when we return key input from the keyboard, the system will enter key as Windows \ r \ n processed, Unix system just as \ n to deal with, no matter what the system, you can use \ n as a line input end of the mark, but we need to pay attention when programming in the Windows system, we will read \ r this character, we must the \ r character and the difference between the normal input open.

    Windows and Unix file format is different, the general is a problem in \ the r \ n problem. A carriage return (CR) and line feed (LF) character is used to represent a "next line". The standard does not specify which one to use. So have the three different uses:

  • windows using carriage return + line (CR + LG) indicating that the next line (i.e., a so-called PC format)
  • UNIX using line feed (LF) indicating that the next line
  • MAC machine carriage return (CR) represented by the next line

When the transfer of files between different systems will involve format conversion.

Conversion between the two file formats:

1、 Unix -> Windows:'\n' -> '\r\n'  

while ( (ch = fgetc(in)) != EOF )

{

    if ( ch == '\n' )

       putchar('\r');

    putchar(ch);

}

Prior to joining '\ n' as long as there is one in the Unix file '\ r' characters on it

2, Unix <-  Windows : '\ n-' <- '\ R & lt \ n-'
from Windows to Unix more complex case, not just read from the file '\ r' can be removed. Because at the end of a line of text Windows file sometimes embedded in a carriage return, this happens in the impact printer. Therefore, before conversion Analyzing '\ r' and if '\ n' simultaneously. If the same time, to remove the '\ r', while if no, retention '\ n'.  

cr_flag = 0;    /* No CR encountered yet */

while ( (ch = fgetc(in)) != EOF )

{

    if ( cr_flag && ch != '\n' ) {

    /* This CR did not preceed LF */

       putchar('\r');

    }

    if ( !(cr_flag = (ch == '\r')) )

       putchar(ch);

}

Guess you like

Origin www.cnblogs.com/bdqczhl/p/11579444.html