Differences between opening a file in text and binary mode

Source: Internet
Author: User
Tags control characters
A text file is also called an ascii code file. It is not the same concept as opening a file in 'text! The text file is stored with the asscii character, that is, it only occupies the binary 0x20 -- 0x7e on the disk. In addition, there are carriage return (0x0d), line feed (0x0a), tab (0x09), and so on, so there is space for compression.

Line breaks and carriage returns are different, and Operating System . '/N' is usually translated into "Line End", that is, LF (line-feed);'/R' is translated into "Carriage Return ", cr (cariage-return)

The carriage return (CR) and line feed (LF) are used to represent the "next line. The standard does not specify which one to use. There are three different usage methods:
(1) DOS and Windows use carriage return + line feed (CR + LG) to represent the next line
(2) UNIX uses the line break (LF) to represent the next line
(3) The Mac machine uses the carriage return (CR) to represent the next line.
When transferring files between different systems, format conversion is required.

The biggest difference between text and binary is that the text has different understandings of '/N' line breaks.
(1) On the DOS platform, the character is expanded to <CR> <LF> two control characters (equivalent to "/R/N"), and 0 DH in the ASCII character set, 0ah
(2) On UNIX platforms, only <LF> is not displayed.
(3) In binary mode, '/N' is precise regardless of the platform. <LF>.

On Linux/Unix systems, there is only one file type system, and the mode with B letters is the same as the corresponding mode without B letters. (UNIX text files do not usually contain Ctrl + Z and/R)

About EOF:
EOF can be used as the end sign of a text file, but cannot be used as the end character of a binary file. the feof function can determine both binary files and text files. EOF is Ctrl + Z in windows, and CTRL + D in Linux.

1. Change the default translation mode directly by setting the global variable _ fmode in your program. The initial default setting of _ fmode is _ o_text, for text mode.

2, _ o_text sets text (translated) mode. carriage Return-linefeed (CR-LF) combinations are translated into a single linefeed character on input. linefeed characters are translated into CR-LF combinations on output. _ o_binary sets binary (untranslated) mode, in which these translations are suppressed. the MS-DOS text file uses a combination of carriage returns and line breaks/R/N to indicate the end of the line. A Macintosh text file uses only one carriage return/R to indicate the end of a line. The C program uses/N to indicate the end of the line. So when a C program opens a MS-DOS text file in text mode, it converts/R/N to/n, when a file is written, it converts/N to/R/n. (If you open the file in binary mode, the C program will see the/R and/n characters .) For the text view of a Macintosh text file, it converts/R to/n when reading the file, and converts/N to/r when writing the file.

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.