Text File and binary file, text file binary

Source: Internet
Author: User

Text File and binary file, text file binary
Here we will talk about the standard interfaces for reading and writing text files, binary files, and C language files.

It is binary for physical storage. The key is that the data encoding is different before storage.


The physical storage of text files and binary files in computer file systems is binary, that is, there is no difference between the physical storage, and there is no objection, their differences are mainly in logical storage, that is, encoding.

When the text file format is stored, the value is used as a character and then stored in its character-encoded binary. The text file uses the 'characters' as the unit to represent and store data. For example, for the value 1, A text file will regard it as the character '1' and save its ASCII code value (here it is assumed that it is ASCII Code). In this way, it is physically the binary value 0x31, and if it is binary, it will save 1, the binary value is saved directly. For example, if the value 1 is an integer in the program, the binary value is 0x00000001 (4 bytes ).


Of course, if the program is originally saved by character, that is, char ch = '1', then the value after binary storage is its ASCII code, because the binary of the variable is its ASCII code. It can be concluded that the binary file is the encoding of the value itself, so it is an indefinite encoding, because the value itself is not a byte, for example, if the integer is 4 bytes, the binary value stored in the binary file is the native binary value of the four bytes.

In summary, we can know that a text file and a binary file are not encoded in the same way. This is a user behavior and the encoding of a data (character or value) the user selects the file to be saved, that is, the interface for writing. If the file is written as a binary interface, it is a binary file, if a file is written as a character, it is a text file. Since there is a write encoding, there will be a read encoding. Only two codes correspond to each other to read the correct result. If you use NotePad to open a binary file, it will be garbled. Here we will slightly mention the suffix, the suffix name cannot be determined whether it is a text file. The binary file can also be a txt suffix name. The suffix name is only used to associate the program to open the program and be used for remarks. It has nothing to do with the specific encoding of the file.

You can use the character interface to read and write binary files. You only need to perform some processing. Therefore, for binary files, text files mainly refer to the reading and writing methods.
In addition, a major difference in windows is that when reading and writing text files, the newline \ n is automatically replaced with \ r \ n.

Finally, text files and binary files are mainly concepts in windows. UNIX/Linux does not distinguish these two files. They treat all files equally and treat them as binary files.

The standard I/O library mainly uses fread/fwrite to read and write binary files, while fread/fwrite fgetc/fputc fprintf can be used for text files.

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.