Streams and files

Source: Internet
Author: User

Flow:

Flow is the way data is transmitted; C programs process a stream instead of directly processing the file. You declare a file *FP, and assign the value returned by fopen (one of the files) to the FP two actions that are equivalent to setting up a faucet,
When you use an input function such as GETC (FP) to read a file character is equivalent to unscrew the tap, each read a character, the file is like water flow, the FP refers to the address of the natural move back one. This process is like the flow of files, abstract as the concept of "flow", in fact, is a way of transmission; According to different classification methods, streams can be divided into input stream, output stream, text stream and binary stream, etc.
A data flow is a set of sequential, starting and ending character sets that is an abstraction of the input and output of information. This is like having two pools, one for storing information, and another for storing programs. There are two pipes in the middle of the two basins, and the water of a pipe can only flow from the pool where the information is stored to the pool where the program is stored, while the water from the other tube can only flow from the pool where the program is stored to the pool where the information The water through the pipe is called the data stream. When input is required, the water in the pool where the information is stored flows through the tube to the pool where the program is stored. When the output is needed, the water in the pond where the program is stored flows through the tube to the pool where the information is stored.

Description: The data flow is divided into input stream (InputStream) and output stream (OutputStream). The input stream can only read data from it, and cannot write data to it. The output stream can only write data to it, and cannot read from it.



File:

"File" refers to an ordered set of related data



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 computer's storage is physically binary, so the difference between a text file and a binary file is not physical, but logical. The two are only different at the coding level.
In short, text files are character-encoded files, common encodings are ASCII-encoded, Unicode-encoded, and so on. Binary files are value-coded files, and you can specify a value (which can be considered a custom encoding) depending on your application.
From the above can be seen that the text file is basically fixed length encoding (also has a non-fixed length encoding such as UTF-8), based on characters, each character is fixed in the specific encoding, ASCII code is 8 bits of encoding, Unicode generally accounted for 16 bits. Binary files can be thought of as variable-length encodings because they are value encodings, and how many bits represent a value that is entirely up to you.
Access

The Text tool opens a file, reads the binary bitstream corresponding to the file physically, and then interprets the stream in the decoded mode of the selection, and then displays the interpretation results. In general, the decoding method you choose will be in ASCII format (one character of ASCII code is 8 bits), Next, it is 8 bits 8 more than specifically to explain this file stream. Notepad no matter what files are opened according to the established character encoding work (such as ASCII code), so when he opened the binary file, garbled is also a very inevitable thing, decoding and decoding does not correspond.
The storage of a text file and its reading are basically a reverse process. and the binary file access and text file access is similar, only the way of encoding/decoding different.
Advantages and Disadvantages

Because the difference between a text file and a binary file is just a different encoding, their pros and cons are the pros and cons of coding. It is generally believed that text file encoding is based on word keystroke length, decoding is easy; binary file encoding is variable length, so it is flexible, storage utilization is higher, decoding is difficult (different binary file format, there are different decoding methods).

Streams and 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.