Text files with binary files

Source: Internet
Author: User
Tags fread

Here we talk about the standard interface between a text file and a binary file and the C language reading and writing these two files.

Specific to the physical preservation is binary, the key is to save the data before the encoding of the difference.


text files and binary files in the computer file system of the physical storage is binary, that is, there is no difference in physical storage is 01 yards, this no objection, their differences are mainly in the logical storage, that is, coding.

When the text file format is stored as a character and then into its character encoded binary, the text file uses ' character ' as the unit to represent and store the data, such as the value 1, the text file will be treated as a character ' 1 ' then save its ASCII encoded value (this is assumed to be ASCII encoding), so that it is physically 0x31 this binary value, and if the binary is saved 1, then directly save the binary value, such as if the program is to process 1 as an integer the binary value is 0x00000001 (4 bytes).


Of course, if the program is stored by character char ch = ' 1 '; The binary saved value is its ASCII code, because the binary of the variable is its ASCII code. Can summarize the binary file is the value of the encoding itself, then is the indefinite length of the code, because the value itself is unequal byte, such as an integer 4 bytes then saved in the binary file is the original binary value of four bytes.

In conclusion, you can know that the text file and binary file is not the same as the encoding, and this is the user behavior, the data in what kind of encoding (character or value itself) into the file is the user actively selected, that is, the interface to write the choice, if the binary interface to write to the file is a binary file, If you write a file as a character, it is a text file. Since there is a write time encoding will have to read the code, only two encoding corresponding to read the correct results, such as open a binary file with Notepad will be garbled, here a little mention of the suffix name, the suffix is not determined whether it is a text file, the binary file can also be the TXT suffix name, The suffix name is only used to associate the open program to the user to do notes, and the specific encoding of the file does not matter.

You can use the character interface to read and write binary files, only need to do some processing, so so-called binary files, text files are mainly embodied in the read-write method here.
In addition, there is a clear difference between windows and the text file read and write, the line will be changed \ n automatically replaced \ r \ n.

The final text files and binaries are primarily Windows-based concepts, and Unix/linux does not differentiate between the two files, which treat all files equally and treat all files as binary files.

The standard I/O library uses fread/fwrite primarily to read and write binary files, while for text files you can use Fread/fwrite FGETC/FPUTC fprintf and so on.

Text files with binary 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.