The following describes a special file operation method-carchive.
Carchive
Introduction:
Allows you to save a complex network of objects in a permanent binary form (usually disk storage) that persists after those objects are deleted.
Carchive
Does not have a base class.
Later you can load the objects from persistent storage, reconstituting them in memory. This process of making data persistent is called "serialization ."
From these words, we can know that carchive uses a buffer zone, that is, a piece of memory space is used as a temporary data storage location, and the read and write operations on carchive are first arranged in this buffer zone, when the buffer zone is full or is required by the user, read and write the data after the entire segment to the specified storage coal. When creating a carchive object, specify whether the mode is used for buffer read or for buffer write.
Generally, there are two ways to create a carchive object:
- Implicitly create a carchive object using the framework
- Create a carchive object explicitly
I.
Create carchive implicitly using the framework
Object
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 carchive object for the document.
The following are the operations performed by the framework when an application user issues the "Save as" command from the "file" menu:
- The "Save as" dialog box is displayed and the file name is obtained from the user.
- Open the file named by the userCfile
Object.
- Create to point to thisCfile
ObjectCarchive
Object. In the createCarchive
The framework sets the mode to "Storage" (that is, writing or serialization), rather than "loading" (that is, reading or deserialization ).
- Call inCdocument
TheSerialize
Function, SetCarchive
Object Reference is passed to this function.
Then, the serialize function of the document writes data to the carchive object (just explained ). When returned from the serialize function, the framework first destroys the carchive object and then destroys the cfile object.
Therefore, if you want the framework to create a carchive object for the document, all you need to do is implement the serialize function for writing the archive and reading the document from the archive. You must also implement serialize for any cobject derived objects serialized directly or indirectly by the serialize function of the document.
Create a carchive object explicitly
In addition to serialization of documents through the framework, carchive objects may also be required in other scenarios. For example, data that may be serialized to or from the clipboard is represented by a csharedfile object. Alternatively, you may need to use the user interface to save files different from those provided by the framework. In this case, you can create a carchive object explicitly. Perform the following procedure in the same way as the framework.
II.
Create carchive explicitly
Object
StructureCfile
Object or slaveCfile
The exported object.
- As shown in the following exampleCfile
ObjectCarchive
Constructor:
Cfile thefile;
Thefile. Open (..., cfile: modewrite );
Carchive archive (& thefile, carchive: Store );
The second parameter of the carchive 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. The serialize function of the object checks the status by calling the isstoring function of the archive object.
When data is stored to or loaded from the carchive object, the object is closed. Although the carchive object (and cfile object) will automatically close the archive (and file), it is good to execute it explicitly because it makes it easier to recover from errors.
Close the carchive object.
Archive. Close ();
Thefile. Close ();
The following is a simple example:
Read files:
Char filename [50]; <br/> cout <"pleast input the file name you want to read:/N"; <br/> CIN> filename; <br/> wchar wszfilename [256]; // Unicode user name <br/> // convert ANSI filename to Unicode <br/> multibytetowidechar (cp_acp, 0, filename, <br/> strlen (filename) + 1, wszfilename, <br/> sizeof (wszfilename)/sizeof (wszfilename [0]); <br/> cfile myfile (wszfilename, cfile: moderead); <br/> carchive AR (& myfile, carchive: load); // select an appropriate method and load as read. <br/> int N; <br/> float F; <br/> cstring STR; <br/> If (AR. isloading () <br/>{< br/> // read through>, the order must be the same as the write order <br/> ar> N> F> STR; </P> <p> cout <n <Endl; <br/> cout <F <Endl; <br/> wcout <(lpctstr) STR <Endl; <br/> // read <br/> char readbuf [100]; <br/> ar. read (readbuf, 100); </P> <p> cout <readbuf; <br/> // readstring <br/> ar. readstring (STR); <br/> wcout <(lpctstr) STR <Endl; <br/> ar. readstring (STR); <br/> wcout <(lpctstr) STR <Endl; <br/>}< br/> ar. close (); <br/> myfile. close ();
Write File:
Cout <"pleast input the file name you want to write:/N"; <br/> char filename [50]; <br/> CIN> filename; <br/> wchar wszfilename [256]; // Unicode user name <br/> // convert ANSI filename to Unicode <br/> multibytetowidechar (cp_acp, 0, filename, <br/> strlen (filename) + 1, wszfilename, <br/> sizeof (wszfilename)/sizeof (wszfilename [0]); <br/> cfile myfile (wszfilename, cfile: modecreate | cfile: modereadwrite ); </P> <p> carchive AR (& myfile, carchive: Store); // select the proper method, and store is write <br/> int n = 10; <br/> float F = 2.4f; <br/> cstring STR ("Hello carchive! "); <Br/> cstring str1 (" string1 "), str2 (" string2 "); <br/> char writebuf [100] =" Write character! /N "; <br/> If (AR. isstoring () <br/>{< br/> // pass <write <br/> ar <n <F <STR; <br/> // write <br/> ar. write (writebuf, 100); <br/> // write str1 and str2 to the archive <br/> ar. writestring (str1); <br/> ar. writestring (_ T ("/N"); <br/> ar. writestring (str2); <br/> ar. writestring (_ T ("/N"); <br/>}< br/> ar. close (); <br/> myfile. close ();
In addition, carchive can also write data such as class objects. We can read and write through writeclass (), writeobject () and supporting readclass () and readobject. Example:
Writeclass ()/readclass ():
Cfile myfile (_ T ("myfile. dat "), <br/> cfile: modecreate | cfile: modereadwrite); <br/> // create a storing archive. <br/> carchive arstore (& myfile, carchive: Store); <br/> // store the class cage in the archive. <br/> arstore. writeclass (runtime_class (CAGE); <br/> // close the storing archive. <br/> arstore. close (); <br/> // create a loading archive. <br/> myfile. seektobegin (); <br/> carchive arload (& myfile, Carchive: load); <br/> // load a class from the archive. <br/> cruntimeclass * Pclass = arload. readclass (); <br/> If (! Pclass-> isderivedfrom (runtime_class (CAGE) <br/>{< br/> arload. Abort (); <br/>}< br/>
Writeobject ()/readobject ():
Cfile myfile (_ T ("myfile. dat "), <br/> cfile: modecreate | cfile: modereadwrite); <br/> Cage age (21), * page; <br/> // create a storing archive. <br/> carchive arstore (& myfile, carchive: Store); <br/> // write the object to the archive <br/> arstore. writeobject (& age); <br/> // close the storing archive <br/> arstore. close (); <br/> // create a loading archive. <br/> myfile. seektobegin (); <br/> carchive arload (& myfile, carchive: load); <br/> // verify the object is in the archive. <br/> page = (cage *) arload. readobject (runtime_class (CAGE); <br/> assert (age = * page); <br/>
Carchive is a very useful File Processing class encapsulated by MFC. We can use it when we need serialized data processing.
For other use and details of the carchive class, refer to the description on msdn.