Http://blog.sina.com.cn/u/548e3c2f010002eg
The following describes how to use the ctypedptrmap pointer template class in programming. The storage structure occupies less physical space and is much smaller than the files stored simply in an array structure, therefore, it is a highly recommended method for storing and reading files.
You must include # include <afxtempl. h> In the stdafx. h file // MFC templates
Include # include <Io. h> in the header file
WhereCBank. hThe file is as follows:
Class cBank: Public cobject
{
Declare_serial (cBank) // declare serialized Storage
Public:
CBank ();
CBank (cstring Str) {m_bankname = STR ;}
CBank (const cBank & B) {m_bankname = B. m_bankname;} // copy the constructor
Virtual ~ CBank ();
PRIVATE:
Cstring m_bankname;
Public:
Virtual void serialize (carchive & AR );
};
Typedef ctypedptrmap <cmapstringtoob,
Cstring,
CBank *> cbanktmp; // This is the type we generate based on this class.
Its implementation file. CppAs follows:
Implement_serial (cBank, cobject, 1) // implement serialized Storage
CBank: cBank ()
{
M_bankname = "";
}
CBank ::~ CBank ()
{
}
Void cBank: serialize (carchive & AR)
{
If (AR. isloading ())
{
Ar> m_bankname;
}
Else
{
Ar <m_bankname;
}
}
/*----------------------------------------------------
Read parameters from a file:
Note: m_map is defined as follows:Cbanktmp m_map;
-------------------------------------------------*/
Void cdlgbankin: loadbankname ()
{
Uint nflags = cfile: typebinary | cfile: modereadwrite;
If (_ access (_ T ("bankname. dat"), 0 ))
{
Nflags | = cfile: modecreate;
}
Cfile file;
Cfileexception Fe;
If (file. Open (_ T ("bankname. dat"), nflags, & Fe ))
{
If (file. getlength () = 0) // if the file is empty, no read operation is performed.
{
}
Else
{
Carchive AR (& file, carchive: load );
M_map.serialize (AR); // read the file
// Fill the list box according to the file content
Position Pos;
Cstring strkey;
CBank * pb = NULL;
For (Pos = m_map.getstartposition (); pos! = NULL ;)
{
M_map.getnextassoc (Pos, strkey, (cBank *) Pb );
M_cmbbank.addstring (strkey );
}
Delete Pb;
}
}
File. Close ();
}
/*-------------------------------------------------
Function: First open the file and check whether the file contains the same content as the keyword in the edit box,
If the content is the same, no content is added. if the content is not the same, the file is written.
-------------------------------------------------*/
Void cdlgbanklist: onbtnadd ()
{
// Todo: add your control notification handler code here
Cstring strkey;
Getdlgitemtext (idc_editbank, strkey );
If (0! = Strkey. getlength ())
{
CBank * pb;
// Open the file
Uint nflags = cfile: typebinary | cfile: moderead;
Cfile file;
Cfileexception Fe;
If (file. Open (_ T ("bankname. dat"), nflags, & Fe ))
{
If (file. getlength () = 0) // if the file is empty, no serialization is performed.
{
}
Else
{
Carchive AR (& file, carchive: load );
M_map.serialize (AR );
}
// Analyze the file and add the content in the edit box to the file
If (! M_map.lookup (strkey, (cBank *) Pb) // you must add
{
If (cb_err! = M_listbox.addstring (strkey ))
{
M_map.setat (strkey, new cBank (strkey); // assign a value to the strkey keyword
}
}
}
File. Close ();
// Store files
Nflags = cfile: typebinary | cfile: modewrite;
If (file. Open (_ T ("bankname. dat"), nflags, & Fe ))
{
Carchive AR (& file, carchive: Store );
M_map.serialize (AR); // serialized Storage
}
}
}
/*-------------------------------------------------
Function: First open the file and then find the keyword in the file,
If this keyword is found, the keyword is deleted, but the content corresponding to the keyword is still not deleted. This is expected to be improved.
-------------------------------------------------*/
Void cdlgbanklist: onbtndel ()
{
// Todo: add your control notification handler code here
Int iindex;
Cstring strkey;
If (lb_err! = M_listbox.getcursel ())
{
// Open the file
Uint nflags = cfile: typebinary | cfile: moderead;
Cfile file;
Cfileexception Fe;
If (file. Open (_ T ("bankname. dat"), nflags, & Fe ))
{
Carchive AR (& file, carchive: load );
M_map.serialize (AR );
Iindex = m_listbox.getcursel ();
M_listbox.gettext (iindex, strkey );
M_listbox.deletestring (iindex );
// Analyze the file and add the content in the edit box to the file
CBank * pb = NULL;
If (m_map.lookup (strkey, (cBank *) Pb ))
{
M_map.removekey (strkey); // Delete the keyword, but the content corresponding to the keyword is not deleted
}
}
File. Close ();
// Store files
Nflags = cfile: typebinary | cfile: modewrite;
If (file. Open (_ T ("bankname. dat"), nflags, & Fe ))
{
Carchive AR (& file, carchive: Store );
M_map.serialize (AR); // serialized Storage
}
}
}