Analysis of the document operation in VC 2002-8-4 17:33:29 Yanshan University Nie Dongdong reading: 18285 Various information about documents is very common in programming, if you can do this, you can find it according to the actual situation. The best solution can be written in a shorter time. This article conducts a comprehensive introduction to the relevant file operations in Visual C , and analyzes some of the difficulties that are often encountered in the file operation. 1. File finding When the file is operated, if you don't know if the file exists, you will first find it. There is a class "cfileFind" specifically used to perform file-looking for file-looking, which can be used to find files quickly and easily. The following code demonstrates the most basic method of use of this class. CFILEFIND FINDER; BOOL BWORKING = Finder.FindFile ("c: //windows//sysbkup//*.cab"); while (bworking) {bworking = finder.findNextFile (); strfileTitle = Finder.GetFileTitle () ; } 2. The Open / Save dialog of the file allows the user to select a file to open / save the dialog box when you select a file to open and store operations. The MFC class "cfiledialog" is used to implement this function. When using "cfiledialog" declares an object, the first BOOL type parameter is used to specify the file opening or saving. When the file is constructed, a file open dialog is constructed, and a file save dialog is constructed for False. When constructing the "cfiledialog" object, if the "OFN_ALLOWMULTITISELECT" style is specified in the parameter, multiple selection operations can be performed in this dialog. At this point, you must pay attention to the "m_ofn.lpstrfile" of this "cfiledialog" object to allocate all the file path names returned by the multi-selection operation. If the memory is not assigned or allocated, the operation failed . The following program demonstrates how the file opens the dialog box. CFileDialog mFileDlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, "All Files (* *) | * * | |..", AfxGetMainWnd ()); CString str ( "", 10000); mFileDlg.m_ofn.lpstrFile = str.GetBuffer (10000); str.ReleaseBuffer (); POSITION mPos = mFileDlg.GetStartPosition (); CString pathName ( "", 128); CFileStatus status; while (! mPos = NULL) {pathName = mFileDlg.GetNextPathName (mPos ); Cfile :: getStatus (Pathname, Status);} 3. The reading and writing of the document is very important, and the focus will be introduced below.
The most common method of reading and writing is to use the "cfile" class directly. If the read and write of the file can use the following method: // Make the files CHAR SREAD [2]; cfile mfile (_t ("user.txt" ), Cfile :: modeRead; if (mfile.getlength () <2) return; mfile.read (Sread, 2); mfile.close (); // Write the file CFILE MFILE (_T ("User. TXT "), cfile :: modewrite | cfile :: modecreate; mfile.write (sread, 2); mfile.flush (); mfile.close (); although this method is most basic, it uses cumbersome, and function very simple. It is recommended here that use "carchive", which is simple and powerful. First, use "cfile" to declare an object, then use this object's pointer to declare a "carchive" object so that you can store a variety of complex data types. Its method is used to see the example: // Write the file CString Strtemp; cfile mfile; mfile.open ("D: //dd//try.try", cfile :: modecreate | cfile :: modenotruncate | CFILE: : ModeWrite; CARCHIVE AR (& Mfile, CARCHIVE :: Store); ar << strtemp; ar.close (); mfile.close (); // read the file CFILE MFILE; if (Mfile.Open " : //dd//try.try ", cfile :: moderad) == 0) Return; CARCHIVE AR (& mfile, carchive :: loading); ar >> strtemp; ar.close (); mfile.close (); "CARCHIVE" "<<" and ">>" operator is used for the read and write of simple data types. For the access to the object of "COBJECT" derived class, readObject () and writeObject () are used.