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 search
When you operate on a file, if you don't know if the file exists, you must 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.
Cstring strfileTitle;
CFILEFIND FINDER;
Bool bworking = finder.findfile ("c: //windows//sysbkup//*.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_HIDEREADOLY | OFN_OVER
Writeprompt | OFN_ALLOWMULTITILECT, "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. Document reading and writing
The reading and writing of the file 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 the file
CHAR SREAD [2];
Cfile Mfile (_T ("User.txt"), cfile :: moderad);
IF (Mfile.GetLength () <2)
Return;
Mfile.read (Sread, 2);
Mfile.close ();
// write a file
CFILE MFILE ("User.txt"), cfile :: modeWrite | cfile :: modecreate;
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 a 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 ("D: //dd//try.try", cfile :: moderad) == 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 the CaboutDLG class
Cruntimeclass * MRunclass = ar.read
Class ();
// Use the 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: //temp//aa.bat", cfile :: modewrite, & mexcept);
CString string = "i am a string.";
Mfile.writestring (String);
Mfile.close ();
4. Temporary document
Formal software often uses temporary files, often see a large amount of extension ".tmp" in the "C: / Windows / Temp" directory, which is a temporary file established when the program is run. 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.