C fopen fread Error

Source: Internet
Author: User

Read a TXT file, and the results were not completed in the morning.

The procedure is as follows:

Can you see where there is a problem? The output string is followed by "tun" or "hot". A single character is also output. Why, search, search, and change the TXT content, still unavailable

Finally, change the r of fopen to RB. The magic is that txt is an ascii code file. Why is it changed to RB? B is binary.

Search again, and the experts in the Forum said:

After the binary is enabled, you can use fgets, and after ASCII is enabled, you can also use fwrite.
The difference is
"The carriage return is a little different. One is 0x0a and the other is 0x0d 0x0a. I cannot remember which one is specific ."

Open in binary mode
1. All characters will be read, including: Carriage Return, line feed, '\ 0'. Reading will not be stopped until the end of the file;
2. You can read all types of files, including: EXE, DLL, OCX ......
Open in ASCII mode when the following characters are encountered:
1. Press enter, line feed, '\ 0', then the stop reading of the current line of text cannot be read until the end of the line, and then read the next line;
2. Only text files can be read correctly.

Two mappings are required:
1 is the correspondence between the operation function and the open mode. For example, if fread/fwrite is used, the file must be operated in binary mode,
If you are using text (ASCII), errors may occur...
The read and write modes of files 2 correspond to each other. When a file is written in binary mode, the read operations are also in binary mode. Otherwise, the data may be different, the source of the difference lies in the difference between the two operation modes: one directly acts as a binary stream (Binary) operation, and the other is an ASCII stream operation )...

What is the difference between binary opening and text opening? It is clearly the difference between DOS and Unix carriage return characters.

Let me sum up: in fact, what is fopen to open a file? "rb" "RT" and "R" are actually non-text files and binary files, but you use binary to open a file, do not use ASCII operation functions such as fputs fgets. You must use fread fwrite.

Similarly, if you open a file using ASCII code, you should not use fread. I just made this mistake.

MARK ::

 

C fopen fread Error

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.