CMemFile Class

The CFile-derived class that supports memory files.

class CMemFile : public CFile

Remarks

These memory files behave like disk files except that the file is stored in RAM rather than on disk. A memory file is useful for fast temporary storage or for transferring raw bytes or serialized objects between independent processes.

CMemFile objects can automatically allocate their own memory or you can attach your own memory block to the CMemFile object by calling Attach. In either case, memory for growing the memory file automatically is allocated in nGrowBytes-sized increments if nGrowBytes is not zero.

The memory block will automatically be deleted upon destruction of the CMemFile object if the memory was originally allocated by the CMemFile object; otherwise, you are responsible for deallocating the memory you attached to the object.

You can access the memory block through the pointer supplied when you detach it from the CMemFile object by calling Detach.

The most common use of CMemFile is to create a CMemFile object and use it by calling CFile member functions. Note that creating a CMemFile automatically opens it: you do not call CFile::Open, which is only used for disk files. Because CMemFile doesn't use a disk file, the data member CFile::m_hFile is not used and has no meaning.

The CFile member functions Duplicate, LockRange, and UnlockRange are not implemented for CMemFile. If you call these functions on a CMemFile object, you will get a CNotSupportedException.

CMemFile uses the run-time library functions malloc, realloc, and free to allocate, reallocate, and deallocate memory; and the intrinsic memcpy to block copy memory when reading and writing. If you'd like to change this behavior or the behavior when CMemFile grows a file, derive your own class from CMemFile and override the appropriate functions.

For more information on CMemFile, see the articles Files in MFC and Memory Management (MFC) and see File Handling in the Run-Time Library Reference.

Requirements

Header: afx.h

See Also

Reference

CFile Class

Hierarchy Chart

Other Resources

CMemFile Members