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.