Why I cannot reuse CMemFile?

  • Thread starter Thread starter tempc
  • Start date Start date
T

tempc

Guest
Hi,

In the following codes, I try to reuse CMemFile by detaching the buffer and re-attache the buffer:

CMemFile MemFile;
CArchive Archive(&MemFile, CArchive::store);
CString strLine, strOutput;
UINT uSize;
BYTE* lpBuf;

for (UINT nIndex = 0; nIndex < 500; nIndex ++)
{
// Reset the file to empty
MemFile.SetLength(0);

strLine.Format(_T("This is line %u."), nIndex);

Archive.WriteString(strLine);

Archive.Flush();

// Get the data out of the memory file
uSize = MemFile.GetLength();
lpBuf = MemFile.Detach();

strOutput = CString((LPCTSTR)lpBuf, uSize / sizeof(TCHAR));

TRACE(_T("%s\r\n"), strOutput);

// Re-attach the buffer so that we can reuse the memory file
MemFile.Attach(lpBuf, uSize, 1024);
}

However, the output is multiple "This is line 0", as below:

This is line 0.
This is line 0.
This is line 0.
...


Why?

Continue reading...
 
Back
Top