After C ++ writes data to a binary file, it opens the file for the second time to modify part of the data. How can the data previously written be deleted.

Source: Internet
Author: User
After C ++ writes data to a binary file, it opens the file for the second time to modify part of the data. How can the data previously written be deleted.

Http://zhidao.baidu.com/question/375750820.html

After C ++ writes data to a binary file, it opens the file for the second time to modify part of the data. How can the data previously written be deleted. This is my file opening mode OUTFILE. Open (filename. c_str (), ofstream: Out | ofstream: Binary );
Question added:
Replace the Read mode with OUTFILE. Open (filename. c_str (), ofstream: Binary | ofstream: In | ofstream: Out). Then you can directly modify the Read mode.

Satisfactory answer
There is a problem with the open method. If the original file exists, the content in the original file will be cleared. If the file does not exist, the new file will be created. Take a closer look at the stream file input and output. We recommend that you read and modify the file content first.

Ofstream flush () function problem http://bbs.chinaunix.net/thread-1242185-1-1.htmlm_pstream-> seekp (m_header.fcontentoffset, IOS: Beg );


M_pstream-> write (const char *) P, Len );
M_pstream-> flush ();

Int posend = m_pstream-> tellp ();

Int iwrtlen = posend-m_header.fcontentoffset;

Assert (iwrtlen = Len );

Return! M_pstream-> fail ();

Find the structure header m_header.fcontentoffset of the file, and write the LEN Length byte from it. After the flush operation, the actual write volume is more than the required write volume. What is the problem? Len = 288404128 bytes, m_header.fcontentoffset = 13032 bytes, while iwrtlen = 288404239, more than 100 bytes.

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.