Analysis of file operations in VC

zhaozj2021-02-16  67

File operations of the VC analysis: Issue Date: December 2001 Day editor:

Contention

Keywords Article The operation of various documents is very common in programming. If you can find the best solutions, you can find the best solution according to the actual situation, and write efficient in a shorter time. The code is thus skilled in mastering file operations. This article will conduct a comprehensive introduction to the 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 that is specifically used to perform file-looking in the MFC, which can be easily and easily out of file. 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. MFC's class cfileDialog is used to implement this function. When using CFiledialog Declare an object, the first BOOL type parameter is used to specify the file's opening or saving, which will construct a file to open the dialog box, constructed a file save dialog for False. When constructing a 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, which is used to store all file path names returned by the multi-selection operation. If the memory is not assigned or allocated, the operation will fail. 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, such as the reading and writing of the file, you can use the following method: // read the file to read the file 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 :: motecreate); mfile.write (Sread, 2); mfile.flush (); mfile.close (); although this method is most basic, its use is cumbersome, and the function is very simple. I recommend it to you that using carchive, it is easy to use and is very powerful. The first is also a target with cfile, and then use the pointer of this object to declare a CARCHIVE object, you can easily store a variety of complex data types. It is found in the following way. // 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 << ar.close (); mfile.close (); // read the file in the file cfile mfile; if (Mfile.Open ("D: //dd/try.try" , CFILE :: ModeRead == 0) Return; CARCHIVE AR (& mfile, carchive :: loading); ar >> strtemp; ar.close (); mfile.close (); carchive << and >> operator Reading and writing of simple data types, access to readObject () and writeObject () for access to COBJECT derived classes. Using carchive readclass () and writeclass () can also make class read and write, such as: // Store CaboutDLG class ar.writeclass (runtime_class (caboutdlg)); // Read CaboutDLG Class cruntimeclass * MRunclass = ar.readclass () ; // Use the CaboutDLG class COBJECT * POBJECT = MRUNCLASS-> CreateObject (); (CDIALOG *) POBJECT -> Domodal (); Although the documentation in the document / view structure provided by the VC can also be made, but it is not easy Understand, use and manage, so although a lot of VC introduction books tell the document / view structure, I suggest you do not use its documentation. There are many books on how to separate documentations / depending on the separation of documentation / view, including very famous "Visual C technology insider". If the file operation you want to perform is just a simple read and write string, I suggest you use CSTDIOFILE to use it to make such actions are very convenient, such as the following example.

转载请注明原文地址:https://www.9cbs.com/read-20233.html

New Post(0)