Differences between text streams and binary streams in C Language

Source: Internet
Author: User

I recently saw the difference between a text stream and a binary stream. The book is vague and obscure, So I carefully thought about it and shared my experiences with you:

I. First answer: What is a file and a stream?

A file is usually a named storage area on the disk. For example, stdio. H is the name of a file containing some useful information.

C regards the file as a continuous byte sequence, and each byte can be read separately. This is consistent with the file structure in the Unix environment (the birthplace of C. Because file models in other environments may be different, ansi c provides two types of views for files: Text View and binary view.

C regards the input and output of data as the inbound and outbound data. Whether it is a disk file or a physical device, it can be regarded as the source and destination of a stream, and they are regarded as the same thing, regardless of its physical structure, that is, its operations are the inbound and outbound data. This type abstracts the input and output operation objects of data into a stream. Regardless of its specific structure, the method is very helpful for programming, and the output operation functions involving the stream can be used for various objects, it has nothing to do with the specific entity, that is, it is universal.

Ii. Differences between Text View and binary View

Each byte in the binary view can beProgram.

In the text view, the content displayed by the program and the content of the file may be different (non-Unix environment ). For example, there is a file test.txt in DOS, whose content is

 
Hello\ R \ nWorld\ R \ n^ Z

When test.txt is opened in binary mode, the content of the C program is the same as the original one (as shown below). At this time, no ing occurs.

 
Hello \ r \ nworld \ r \ n^ Z

When test.txt is opened in text mode, the C program will see the following content: (Note the end of the line and the end of the file)

 
Hello\ NWorld\ N

That is to say, when reading a file using the text view, the local environment representation at the end of the line will be mapped to the C view; in the output, the end of the line in view C is also mapped to the local environment representation. In this way, C uses the text view to skillfully convert files in different environments (DOS, Mac) to the same representation (UNIX Notation ).

The binary view is unchanged from the original file! I understand it a bit, right?

 

3. Text and binary files

Text files are character-encoded files with fixed length. Common encodings include ASCII and Unicode. Binary files are value-encoded files. They are variable-length encoded. You can specify the meaning of a value based on the specific application (such a process can be considered as custom encoding ).
What is the process of opening a file using a text tool? Taking notepad as an example, it first reads the binary bit stream corresponding to the file physically, then explains the stream according to the decoding method you selected, and finally displays the interpretation result. When you use NotePad to open a binary file, Garbled text is inevitable because it is coded according to the established characters (such as ASCII code.

The encoding of text files is based on the Character length, which makes decoding easier. binary files are longer encoded. Therefore, the encoding is flexible, the storage utilization is higher, and the decoding is more difficult (different binary file formats, there are different decoding methods ). The readability of text files is better, and the storage takes time to convert (code compilation is required for reading and writing), while the readability of binary files is poor (specific file decoder is required ), the storage does not have the conversion time (do not codec for read/write, write the value directly ).

Do you understand?

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.