About text files and binary files and text streams and binary streams (Final Version)

Source: Internet
Author: User

1) Introduction to text files and binary files
The so-called text file stores all characters in the file. For example, a string 123 is stored in the text file in its ASCII form, that is, the ASCII form of each character:
31 32 33 is stored in text files for example, for an integer data 123: 31 32 33 is the ASCII format of each character. The so-called binary file is stored in binary format, for a string 123, the storage format in the binary file is to convert its ASCII code to the binary format: Its ASCII is 31 32 33, and then the ASCII code is converted to binary, that is: 00011111 00100000 00100001 form. For an integer data 123 stored in a binary file, the binary format is 123: 00000000 00000000 00000000 01111011.

This is just what we can understand from the form of files, but the memory in the computer is stored in binary form. The reason why we can see the content in the text file is that the text file uses ASCII decoding, the binary file uses the binary decoding format! So sometimes, when we use a notepad to open a binary file, we will see a lot of things we don't want to see, which is garbled! (They adopt different decoding forms)

In the brief introduction above, we can read binary files by words, which saves space than text files, because text files store a single character. For example, for an integer data of 2147483647, A text file occupies 10 bytes of memory, while a binary file only occupies 4 bytes of memory, that is, the length of integer data!

This stream is divided into two types in the computer: Text Stream and binary stream. Here we have to make it clear, do not mistakenly think that text files must be opened in the form of text streams, while binary files must be opened in the form of binary streams. This is a big mistake !! The so-called decoding and storage methods are different for text files and binary files. Any file can be opened through text streams and binary streams! We have to figure out this 1.1 million!

There is an important difference between a text stream and a binary stream.

The test code is as follows:

# Include "stdafx. H "# include <stdio. h> # include <direct. h> int _ tmain (INT argc, _ tchar * argv []) {char lacfilename [512] = {0}; memset (lacfilename, 0, sizeof (lacfilename )); file * ptxtfile = NULL; If (getcwd (lacfilename, 512 )! = NULL) {strcat (lacfilename, "\ binary write difference .txt"); ptxtfile = fopen (lacfilename, "W + B"); int DATA = 0x0a; fwrite (char *) & Data, 1, sizeof (INT), ptxtfile); fclose (ptxtfile);} return 0 ;}

The test results are as follows:

First, in Unix systems, each line ends with "<line feed>", that is, "\ n". In Windows systems, each line ends with "<press enter> <line feed> ", that is, "\ r \ n". In MAC systems, the end of each line is "<press enter> ". Therefore, during file read/write, if the file is opened in text mode and the value 0x0 A is written to the file, the Windows system recognizes it as the return key during file storage, and automatically write 0x0d (\ r: line feed key ). If opened in binary mode, only 0x0a is written. One direct consequence is that if a file in UNIX/MAC is opened in windows, all the text will
And Windows files opened under UNIX/Mac, there may be an extra ^ m symbol at the end of each line.

When a file is opened as a text stream, the 0d 0a combination stored continuously in the file will be converted into a character 0a, that is, a line break !! If an object is opened as a binary stream, the preceding situation does not occur. Instead, it reads one character and one character, that is, how many characters can I read, I want to convert the 0d 0a combination stored in a row to a character 0a. When this combination is encountered, the 0d 0a is directly read.

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.