Fopen parameter in C language-binary and text

Source: Internet
Author: User

Debugging yesterdayProgramIt is found that the file generated by calling a copy function is always inconsistent with the source file, so I checked the copy function and finally found out the problem. The fopen file was not opened in binary mode, the default mode is text.

 

To convert it to the binary mode and to the text mode.

 

Differences between fopen text files and binary files

 

Http://hi.baidu.com/assslove/blog/item/d0b727ea014ee3dbd539c9e8.html

 

After learning C language file operations, we will know that the function for opening a file is fopen, and its second parameter is a flag string. If 'B' appears in the string, it indicates that the binary file is opened; otherwise, the text file is opened.

So what are text files and binary files? Most people may have not considered it carefully.

In Windows and DOS systems, a narrow text file refers to a file with a txt extension. In fact, all files in the unspecified format that consist of understandable ASCII and other encoded texts are text files, such as C source files, HTML Hypertext files, and XML files. Other files are binary files, such as Word file Doc and Image Format File JPG.

However, the so-called use of the fopen flag to open text files and binary files is not accurate. The correct statement should be: open the file in text and binary mode. Because we can use any file in both ways.

Even so, why do we need to distinguish between the two methods?

This is because the two methods perform different operations when reading and writing files.

The binary method is very simple. When reading a file, it will read all the content of the file in an intact manner. When writing, it also writes the content of the memory buffer to the file.

The text mode is different. When writing a file, the newline CRLF (0x0d 0x0a) is converted into a single 0x0a, and when the terminator ctrlz (0x1a) is encountered, it is deemed that the file has ended. Correspondingly, when writing a file, all 0x0a is replaced with 0x0d0x0a.

Therefore, if you use text to open a binary file, it is easy to see incomplete file reading or wrong content. Even if you use text to open a text file, you should be cautious when using it. For example, if you copy a file, you should not use text.

Note that the above statements only apply to DOS and Windows systems. In UNIX and other systems, there is no distinction between the text method and the binary method, so that the use of the 'B' flag is the same. This is because the text file line breaks are defined by different operating systems, which is different from the line breaks defined in C language.

As mentioned above, DOS and Windows systems use the double byte CRLF (0x0d 0x0a) as text file line breaks, while UNIX text files only have one byte LF (0x0a. In the C language, the line break is also lf or '/N.

Because DOS/Windows-defined line breaks are inconsistent with those defined in C language, CRLF-> lf conversion is applicable when the standard input/output functions of C language are suitable for reading and writing text files. The definition of UNIX is the same as that of C.

So why are there definitions inconsistent? This is purely a historical reason. At the beginning, C was developed on UNIX, and the definition of line breaks is naturally the same. Later, C was introduced to the DOS system. In order to enable the original C program to read and write dos text files without modification, it was modified on file read and write. As DOS/Windows became the mainstream platform, the modifications made for compatibility added such a small amount of trouble to many C language developers.

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.