Summary of [Press enter] [line feed] About text files in C Language

Source: Internet
Author: User

<Press enter>: \ r in C, 0d in ASCII code, Cr in symbol, and carriage return

<Line feed>: \ n in C, 0a in ASCII code, lf in line feed

<! -- [If! Supportlists] -->I,<! -- [Endif] -->Status quo

In Unix systems, each line ends with only "<line feed>", that is, "\ n ";

In Windows, each line ends with "<press enter> <line feed>", that is, "\ r \ n ";

In MAC systems, each line ends with "<press enter>", that is, "\ r ";

 

<! -- [If! Supportlists] -->II,<! -- [Endif] -->Impact

<! -- [If! Supportlists] -->1.
<! -- [Endif] -->Direct Performance

If a file in a UNIX/MAC system 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.

<! -- [If! Supportlists] -->2.
<! -- [Endif] -->Read/write in Windows

Write "\ n" to the file ".

<! -- [If! Supportlists] --> 2 <! -- [Endif] --> Use fopen ("test1.txt", "W"), which is 2 bytes (0d 0a ).

<! -- [If! Supportlists] --> 2 <! -- [Endif] --> Use fopen ("test2.txt", "WB"), which is 1 byte (0a)

<! -- [If! Supportlists] --> 2 <! -- [Endif] --> Use createfile ("test3.txt", generic_write ,...), 1 byte (0a)

Read from a file (0d 0a)

<! -- [If! Supportlists] --> 2 <! -- [Endif] --> Use fopen ("test1.txt", "R"), which is 1 byte (0a ).

<! -- [If! Supportlists] --> 2 <! -- [Endif] --> Use fopen ("test2.txt", "rb"), which is 2 bytes (0d 0a)

<! -- [If! Supportlists] --> 2 <! -- [Endif] --> Use createfile ("test3.txt", generic_read ,...), 2 bytes (0d 0a)

 

<! -- [If! Supportlists] -->3.
<! -- [Endif] -->When dividing strings Based on '\ n', note that

For use of fopen ("test2.txt", "rb") and createfile ("test3.txt", generic_read ,...) When reading data, note the impact of (0d 0a) (that is, an 0d more.

 

<! -- [If! Supportlists] -->4.
<! -- [Endif] -->Others

To be supplemented...

 

<! -- [If! Supportlists] -->III,<! -- [Endif] -->Use of editing tools

<! -- [If! Supportlists] -->1.
<! -- [Endif] -->Nodepad ++

How to display Cr and LF: [View]-> [show end character] or [show all characters ].

<! -- [Endif] -->

Set the default enter key format: [Format]-> [convert to Windows format] or [convert to Unix] or [convert to Mac format]

<! -- [If! Supportlists] -->2.
<! -- [Endif] -->Ultra Edit

[Advanced]-> [configuration]-> [Text Processing]-> [DOS/Unix | Mac processing]

 

<! -- [If! Supportlists] -->IV,<! -- [Endif] -->Source

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.

 

Original article reproduced from: www.cppblog.com/longshen/archive/2011/10/31/159399.html

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.