if (!pFile->Open ("foo.dat", CFile::modeReadWrite |
CFile::shareExclusive)) {
// Handle error
return;
}
Step 2 - Hook up the archive
Next, a CArchive object is hooked up to the le. The archive provides an e+cient
conduit to persistent storage. Instead of directly reading and writing the le, you
serialize data to and from the archive. The archive needs to know if you're going
to be using it to read or write data. In this example, we'll assume we're writing
data.
// Create archive ...
bool bReading = false; // ... for writing
CArchive* pArchive = NULL;
try
{
pFile->SeekToBegin();
UINT uMode = (bReading ? CArchive::load : CArchive::store);
pArchive = new CArchive (pFile, uMode);
ASSERT (pArchive != NULL);
}
catch (CException* pException)
{
// Handle error
return;
}
Step 3 - Serialize the object
Finally, we serialize the object by calling its serialize() method. serialize()
is just a method we made up. It has nothing to with MFC's
CObject::Serialize(). Also, you don't have to derive your object from
CObject. Our serialize() method takes a pointer to a CArchive and returns
an integer status.
int CFoo::serialize
(CArchive* pArchive)
{
int nStatus = SUCCESS;
评论2
最新资源