File storage and read Analysis

Source: Internet
Author: User

During programming, it is common to read files. Generally, a TXT file is operated. There are two methods to store the file content,Binary and ASCII (Other encoding methods include ASCII)..TXT files are stored as asciifiles. For example, the contents in 1.txt files are 123. After fopen ("1.txt"," R ") is read, the three numbers are read and exist in char C [3]. Therefore, printf (" % C ", c [0]), a character 1, printf ("% d", C [0]) will be output. the result is accidentally 49 (this is the character 1 in ASCII ).

As to why does 1 in a text output different methods, it is generally easy for beginners to get dizzy, not why 1 is changed. As a result, the encoding method mentioned above is different. How does one put the "1" on the hard disk? The real storage method is 00110001 (49 ). Note the differences between the above two outputs: % C, % d. The key is to checkProgramHow the clerk wants to output it. The character is 1, and the digit is 49. The beginner is dizzy again here. Why does 1 use 00000010 for storage? This is not easy to mess up, it is a binary storage method, but if you use the binary method, you will find that the computer can only display numbers, but nothing else. Our world can be displayed not only by numbers, but also by characters..

How can we express characters? This requires the use of standard, ASCII is the standard of the national standard. It is stipulated that 00110001 (49) is 1 when reading characters. Of course, we can also write a standard that 00110001 (49) represents a (or any symbol ). The problem is that the custom rules are not followed. The subsequent Unicode is actually the same as ASCII, but it is only standard. Communication will not go wrong if the standard is the same.

After understanding the principle of file storage, we can write the file by ourselves. If we generate a 1. xwy file, simply put it in 123. Here is the key. How to store it? If it is binary, the content stored in the file on the hard disk is 00000001 00000010 00000011. If the character is 00110001 (49) 00110010 (50) 00110011 (51. When reading data, as long as the storage method is the same, the content is the correct solution. A complicated file is actually like this, but there are a lot more other content, such as header files and offsets. To put it bluntly, as long as the storage standard is available, you can simply read it using fopen ("file name", "R") programming.

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.