How to Access CMemFile Directly When Using It with CArchive

ID: Q132079

1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 2.20 4.00 4.10

WINDOWS             | WINDOWS NT
kbprg

The information in this article applies to:

SUMMARY

When using CMemFile in conjunction with CArchive, problems may arise from calling CArchive::GetFile and subsequently using the CFile pointer returned for direct file access.

This is true for CFile and any class derived from CFile. Because CArchive buffers I/O, you need to call CArchive::Flush() before using any of the members of CFile.

MORE INFORMATION

When a CMemFile object is used with a CArchive object, the two objects share a single buffer. Both objects retain separate pointers to this shared buffer. It is possible for the two objects to become out of synch if the file is accessed directly through a file pointer retrieved by calling CArchive::GetFile.

For example, say you open a CMemFile, and start writing to it using the serialization functions. Then, suppose you call CMemFile::GetFile, and write to the file using CFile::Write. The first thing that the CMemFile object does is make sure it has enough space for the pending write. For this example, assume that there is not enough space to satisfy the write. This means that the CMemFile object must reallocate the buffer. This will most likely change the location of the buffer. The problem occurs when you write to the CMemFile again through the CArchive object. The CArchive object still has a pointer to the old buffer, because the object has no way of knowing that the CMemFile reallocated the shared buffer. Any attempt to write through this pointer does not write to the appropriate file. Therefore, the data is missing from the file where it should have been written.

Additional reference words: kbinf 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00 3.10 3.20 2.20 4.00 4.10 KBCategory: kbprg KBSubcategory: MfcFileIO

Keywords          : kbFileIO kbMFC kbVC 
Version           : 1.00 1.50 1.51 1.52 | 1.00 2.00
Platform          : NT WINDOWS

Last Reviewed: October 11, 1997