VC ++ CArchive and simple file operations,
The CArchive method is used to access files.
I recommend using CArchive, which is simple and powerful. First, you should declare an object with CFile, and then use the pointer of this object as a parameter to declare a CArchive object, so that you can easily store various complex data types. See the following 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 ("d :\\ dd \ try. TRY ", CFile: modeRead) = 0) return; CArchive ar (& mFile, CArchive: load); ar> strTemp; ar. close (); mFile. close ();
The <and> operators of CArchive are used for reading and writing simple data types. For the access to objects of the CObject derived class, ReadObject () and WriteObject () are used (). The ReadClass () and WriteClass () of CArchive can also be used to read and write classes, such:
// Store CAboutDlg class ar. writeClass (RUNTIME_CLASS (CAboutDlg); // read the CAboutDlg class CRuntimeClass * mRunClass = ar. readClass (); // use the CAboutDlg class CObject * pObject = mRunClass-> CreateObject (); (CDialog *) pObject)-> DoModal ();
Although the documents/visual structures provided by VC can perform these operations, they are not easy to understand, use, and manage, therefore, although many VC entry books spend a lot of time describing documents/visual structures, I suggest you do not use their documents. There are many books on how to separate documents/views, including the well-known Visual C ++ technology insider.
If the file operation you want to perform is a simple string for reading and writing the entire line, we recommend that you use CStdioFile to perform such operations conveniently, as shown 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. Use of temporary files
Regular software often uses temporary files. you can often see a large number of files with the tmp extension under the C: \ Windows \ Temp directory. These files are temporary files created by the program running. The usage of temporary files is basically the same as that of conventional files, but the file name should be obtained by calling the GetTempFileName () function. The first parameter is the path of the temporary file, the second parameter is the prefix of the temporary file name, and the fourth parameter is used to obtain the temporary file name. After obtaining the temporary file name, you can use it to create and operate the file, for example:
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. Copy and delete files
MFC does not provide the function of directly performing these operations, so you need to use the SDK. Commonly used file-related functions in the SDK include CopyFile (), CreateDirectory (), DeleteFile (), and MoveFile (). Their usage is simple. For details, refer to MSDN.
1. Determine whether the file exists
_access(filename,mode);
2. For file operations with different purposes, the API function CreateFile () is also a useful method for processing. For most of the other top-level operations that are suitable for giant files, no.
[1] display dialog box, get the file name
CString FilePathName; CFileDialog dlg (TRUE); // if (dlg. doModal () = IDOK) FilePathName = dlg. getPathName ();
Related information: Several member functions used by CFileDialog to retrieve file names:
Assume that the selected file is C: \ WINDOWS \ TEST. EXE.
Then (1) GetPathName (); takes the full name of the file name, including the complete path. Retrieve C: \ WINDOWS \ TEST. EXE
(2) GetFileTitle (); get the full name of the file: TEST. EXE
(3) GetFileName (); Retrieve TEST
(4) GetFileExt (); obtain the extension EXE
[2] opening a file
CFile file ("C: \ HELLO. TXT", CFile: modeRead); // read-only mode
// CFile: modeRead can be changed to CFile: modeWrite (write only ),
// CFile: modeReadWrite (read/write), CFile: modeCreate (new)
Example:
{
CFile file;
File. Open ("C: \ HELLO. TXT", CFile: modeCreate | Cfile: modeWrite );
.
.
.
}
[3] Moving file pointers
File. Seek (100, CFile: begin); // move down 100 bytes from the beginning of the file header
File. Seek (-50, CFile: end); // move up to 50 bytes from the end of the file
File. Seek (-30, CFile: current); // move up 30 bytes from the current position
File. SeekToBegin (); // move to the file header
File. SeekToEnd (); // move to the end of the file
[4] Reading and Writing files
Read files:
Char buffer [1000];
File. Read (buffer, 1000 );
Write File:
CString string ("self-improvement ");
File. Write (string, 8 );
[5] Close a file
File. Close ();