File operation

zhaozj2021-02-16  62

Various information about documents is very common in programming. If these operations can be found, the best solution can be found according to the actual situation, so that high efficiency code 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 BOOL BWORKING = Finder.FindFile ("c: windowssysbkup * .cab"); while (bworking) {bworking = finder.findnextFile (); strfileTitle = Finder.getFileTitle ();}

2. Open / save dialog box

Let the user select the file to open and store operations, use the file to open / save the dialog box. 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_OVER WRITEPROMPT | 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 way to read and write the file is to use the "cfile" class directly, such as the reading and writing of the file, you can use the following method:

// Read operation of the file char Sread [2]; cfile mfile ("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, it is cumbersome, and it is 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. It is found in the following example:

// Write the file CString Strtemp; CFile Mfile; Mfile.Open ("D: DDTRY.TRY", CFile :: MODECREATE | CFILE :: MODENOTRUNCATE | CFILE :: ModeWrite; CARCHIVE AR (& Mfile, Carchive :: Store ); ar << strtemp; ar.close (); mfile.close (); // reads the file to read the file cfile mfile; if (Mfile.Open ("D: DDTRY.TRY", CFile :: ModeRead) == 0) RETURN; CARCHIVE AR (& mfile, cachive :: 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. Use "carchive" readclass () and writeclass () can also read and write classes, such as:

// store CAboutDlg class ar.WriteClass (RUNTIME_CLASS (CAboutDlg)); // read class CAboutDlg CRuntimeClass * mRunClass = ar.Read Class (); // Use CAboutDlg class CObject * pObject = mRunClass-> CreateObject (); (( CDIALOG *) POBJECT) -> Domodal ();

Although documentation in the document / view structure provided by VC can also be made, it is not easy to understand, use and manage, and if the file operation to be performed is just a simple read and write string, it is recommended to use "cstdiofile", with It is very convenient to do such operations, as in the following example:

Cstdiofile mfile; cfileException mexcept; mfile.open ("D: Tempaa.bat", cfile :: modewrite, & mexcept; cstract string = "i am a string."; Mfile.writestring (String); mfile.close ();

4. Temporary document

Formal software often uses temporary files, often see a large number of files named ".tmp" in the "C: WindowsTemp" directory, which is a temporary file established when running. The use of temporary files is basically the same as a regular file, but the file name should call the function getTempFileName (). Its first parameter is the path to establish this temporary file, the second parameter is the prefix of the establishment of a temporary file name, and the fourth parameter is used to get the established temporary file name. After getting this temporary file name, you can use it to create and operate files, such as:

char szTempPath [_MAX_PATH], szTempfile [_MAX_PATH]; GetTempPath (_MAX_PATH, szTempPath); GetTempFileName (szTempPath, _T ( "my _"), 0, szTempfile); CFile m_tempFile (szTempfile, CFile :: modeCreate | CFile :: modeWrite); Char m_char = 'a'; m_tempfile.write (& M_CHAR, 2); m_tempfile.close (); 5. Document copy, delete, etc.

There is no functionality to perform these operations directly in the MFC, so the SDK is used. File related functions in the SDK are common (), createDirectory (), deletefile (), movefile (). They use very simple, refer to MSDN.

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

New Post(0)