Source: Flying Pigeon Book binaries binary file MFC CArchive implementation Save
Flying Pigeon book has been constantly updated in the continuous development, of course, MFC CArchive implementation of saving to binary files is essential. Look at the effect chart first
1 BOOL save2file () 2 {3 bool bopenfiledialog = false; ----------------------------- -----①4 bool Bvistastyle = false; &nb sp; //No vistastyle 5 tchar based_code szfilter[] = ------------------ --------------②6 _t ("Binary file (*.dat) |*.dat|") 7 & nbsp;_t ("All Files (*.*) |*.*| |"); 8 cstring Path_and_filename; 9 10 cfiledialog Fdlg (Bopenfiledialog, _t ("Dat"), _t ("*.dat"),-------③11 & nbsp; Ofn_hidereadonly | Ofn_overwriteprompt, Szfilter, NULL, 0,12 bvistastyle);13 & Nbsp; if (Idok = = Fdlg. DoModal ()) {14 cfile filesave;15 cfilEexception ex;16 17 path_and_filename = Fdlg. GetPathName ();18 if (!filesave.open path_and_filename, cfile::modecreate | 19 cfile::modewrite | Cfile::typebinary, &ex)) { ------④20 TCHAR szerror[ 1024];21 Ex. GetErrorMessage (szerror, 1024);22 MessageBox (NULL, Szerror, _t ("hint "), MB_OK);23 return false;24 }25 carchive ar (&filesave, Carchive::store); ---------------------⑤ 26 for (int i=0;i <<databuf;= ""------------------------------------------⑥28= ""}29= "" Ar.cloSe (); 30= "" Filesave.close (); 31= "}32=" "33=" "return=" "true;34=" "}[=" "size][attachimg]18[=" "attachimg"
①bopenfiledialog is False, the pop-up dialog box is file Save As, and if true, the pop-up dialog box is file Open;
② file Type filter string, this is only in the saved file type of the optional content, file filtering to be set in the first ③;
③_t ("Dat"), _t ("*.dat") sets the file filter type with the default filename, which displays only filtered files when selected.
The ④cfile::typebinary setting allows the binary to open the file;
⑤ creates a serialized object from an open file, associates ar with FileSave, and sets the Carchive::store
⑥ Direct write to AR is the binary way to write to the file Ar<<databuf, then pay attention to close: [size][= "" Align]
"Ar.close (); [= "" Filesave.close (); [= "" Align]