MFC provides the carchive class to implement data buffer read and write, and defines the storage and read solutions for class objects.
There are two ways to create a carchive object:
- Implicitly create a carchive object using the framework
- Create a carchive object explicitly
Implicitly create a carchive object using the framework
The most common and easiest way is to make the framework represent the "save", "Save as", and "open" commands on the "file" menu to create a documentCarchiveObject.
The following are applicationsProgramWhen a user issues the "Save as" command from the "file" menu, the operations performed by the framework:
- The "Save as" dialog box is displayed and the file name is obtained from the user.
- Open the file named by the userCfileObject.
- Create to point to thisCfileObjectCarchiveObject. In the createCarchiveThe framework sets the mode to "Storage" (that is, writing or serialization), rather than "loading" (that is, reading or deserialization ).
- Call inCdocumentThe
Serialize
Function, SetCarchiveObject Reference is passed to this function.
ThenSerialize
Function writes dataCarchiveObject (just explained ). SlaveSerialize
When a function is returned, the framework is destroyed first.CarchiveObject, and then destroyCfileObject.
Therefore, if you want the framework to create a documentCarchiveObject. All you need to do is to write files to and read files from the archive.Serialize
Function. You must alsoSerialize
Any function that serializes directly or indirectlyCobjectImplementation of derived objectsSerialize
.
Create a carchive object explicitly
In addition to serialization of documents through the framework, it may also be required in other scenariosCarchiveObject. For example, data that may be serialized to or from the clipboardCsharedfileObject Representation. Alternatively, you may need to use the user interface to save files different from those provided by the framework. In this case, you can explicitly createCarchiveObject. Perform the following procedure in the same way as the framework.
Create a carchive object explicitly
- StructureCfileObject or slaveCfileThe exported object.
- As shown in the following exampleCfileObjectCarchiveConstructor:
Cfile thefile; thefile. Open (..., cfile: modewrite); carchive archive (& thefile, carchive: Store );
CarchiveThe second parameter of the constructor is to specify whether the archive will be used to store data in the file or the enumerated values used to load data from the file. ObjectSerialize
The function callsIsstoringFunction to check the status.
WhenCarchiveWhen the object stores data or loads data from the object, close the object. AlthoughCarchiveObject (andCfileObject) will automatically close the archive (and file), the good way is to explicitly execute, because this makes it easier to recover from errors.
Disable carchive objects
Refer to msdn, not created by myself.