Fopen WB Parameters

Source: Internet
Author: User

The first parameter is the name (PATH) of the file to be opened, and the second parameter is the mode used to open the file. I didn't quite understand the "W", "WB", "r", and "rb" of the Pattern Parameters at first. According to the textbook, "W" is written in the form of text, while "WB" is written in binary mode. this explanation is a bit mysterious and cannot be understood accurately.

Recently I wrote a small program to store the data in a file in hexadecimal format. when reading the data from the file, I found that the data I read is incorrect. When I debug the program, I found that the read data is not correct with the data written into the file, and debugging found that there is no error during writing. How can I get wrong during reading? After debugging for half a day, we find that each time 0x0a is encountered, there will be an additional 0x0d in front of it.

So why? After several twists and turns, we finally found that the fopen parameter was caused. if you write this statement: fp = fopen (STR, "WB"); normal data can be obtained, while fp = fopen (STR, "W"); normal data cannot be obtained, when 0x0a is encountered, an 0x0d is added before it.

Originally, if the parameter B is enabled in binary mode, the conversion of "Carriage Return" and "linefeed" is not performed in this mode. if there is no B, that is, if the single parameter is style = "font-family: Times New Roman; MSO-bidi-font-size: 10.5pt"> "W, the system converts the linefeed 0a to 0d0a, because Windows uses 0d0a as the linefeed.

At this point, I finally figured out the difference between binary open and text open.

 

Article Source: DIY tribe (http://www.diybl.com/course/3_program/c++/cppjs/20081117/151406.html)

Article Source: DIY tribe (http://www.diybl.com/course/3_program/c++/cppjs/20081117/151406_2.html)

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.