Basic knowledge
MFC CFile class read and write files
The CFile class provides basic functions for file operations such as opening, closing, reading, writing, deleting, renaming, and obtaining file information, which is sufficient to handle any type of file operation.
An example of reading and writing files:
File I / O
Although using the built-in serialization function of the CArchive class is a convenient way to save and load persistent data, sometimes you need more control over the file processing process in your program. For this type of file input / output (I / O) service Requirements, Windows provides a series of related API functions, and MFC encapsulates it into a CFile class, which provides the basic functions of file operations such as opening, closing, reading, writing, deleting, renaming, and obtaining file information. Enough to handle any type of file operation. The CFile class is the base class of the MFC file class and supports unbuffered binary input and output. It can also support buffered serialization of MFC objects through the use of the CArchive class.
CFile class contains a public data member m_hFile, this data member contains the file handle associated with the CFile class object. If no handle is specified, the value is CFile :: hFileNull. Because the significance of this data member depends on the derived class, m_hFile is generally not recommended.
打开 CFile can be used to open a file in two ways: one way is to first construct a CFile class object and then call the member function Open () to open the file, and the other way is to directly use the CFile class constructor to open a file. The following statements demonstrate the process of opening the disk file "C: /TestFile.txt" with these two methods:
C ++ code
1. // Construct an instance first, and then open the file
2.CFile file;
3.file.Open ("C: //TestFile.txt", CFile :: modeReadWrite);
4 .......
5.// Open the file directly through the constructor
6.CFile file ("C: //TestFile.txt", CFile :: modeReadWrite);
The parameter CFile :: modeReadWrite is the mode flag for opening the file. There are a dozen similar flags in the CFile class. The list is as follows:
File mode flag Description
FileCFile :: modeCreate opens the file in create mode, if the file already exists, set its length to 0
FileCFile :: modeNoInherit does not allow inheritance
FileCFile :: modeNoTruncate When creating a file, do not truncate the file if it already exists
FileCFile :: modeRead Open file in read-only mode
FileCFile :: modeReadWrite open file for reading and writing
FileCFile :: modeWrite write open file
FileCFile :: shareCompat allows other processes to open files at the same time during use
FileCFile :: shareDenyNone allows other processes to read and write files during use
FileCFile :: shareDenyRead does not allow other processes to read the file during use
FileCFile :: shareDenyWrite does not allow other processes to write to the file during use
FileCFile :: shareExclusive cancel all access to other processes
FileCFile :: typeBinary Set the file to binary mode
FileCFile :: typeText Set the file to text mode
These flags can be used simultaneously by the "OR" operator to meet multiple needs. For example, a file needs to be opened for reading and writing. If the file does not exist, a new one is created. If the file already exists, the file length is not truncated to 0. To satisfy this condition, several file mode flags such as CFile :: modeCreate, CFile :: modeReadWrite, and CFile :: modeNoTruncate can be used to open the file:
C ++ code
1.CFile file ("C: //TestFile.txt", CFile :: modeCreate | CFile :: modeReadWrite | CFile :: modeNoTruncate);
打开 When the opened file is no longer used, it needs to be closed, which can be closed by the member function Close () or by the destructor of the CFile class. When the latter method is adopted, if the file has not been closed, the destructor will be responsible for implicitly calling the Close () function to close the file, which also indicates that the CFile class object created on the heap will be automatically closed after it is out of scope . Because the object's destructor is called, the CFile object is also destroyed when the file is closed, and the CFile object still exists after the file is closed using Close (). Therefore, after you explicitly call the Close () function to close a file, you can continue to use the same CFile object to open other files.
File reading and writing is the most commonly used file operation mode, which is mainly implemented by CFile class member functions Read () and Write (). Its function prototypes are:
C ++ code
1.UINT Read (void * lpBuf, UINT nCount);
2.void Write (const void * lpBuf, UINT nCount);
The parameter lpBuf is a pointer to the buffer where the data is stored. NCount is the number of bytes to be read or written. Read () returns the number of bytes actually read. The end of the file has been read. You can end the file reading. If you continue reading, 0 will be returned. Therefore, whether the actual number of bytes read is less than the specified number of bytes or whether it is 0 can be used as the basis for judging whether the file read reaches the end. The following code demonstrates the process of writing to the file once and reading repeatedly in a loop:
C ++ code
1. // Create and write files to open files
2.CFile file;
3.file.Open ("C: //TestFile.txt", CFile :: modeWrite | CFile :: modeCreate);
4.
5.// Write to file
6.memset (WriteBuf, 'a', sizeof (WriteBuf));
7.file.Write (WriteBuf, sizeof (WriteBuf));
8.
9.// close the file
10.file.Close ();
11.
12. // Open the file in read-only mode
13.file.Open ("C: //TestFile.txt", CFile :: modeRead);
14.while (true)
15. {
16. // read file data
17.int ret = file.Read (ReadBuf, 100);
18. ……
19. // abort loop if end of file is reached
20.if (ret <100)
21.break;
twenty two.}
twenty three.
24.// close the file
25.file.Close ();
The Write () and Read () functions will automatically move the file pointer after execution, so there is no need to explicitly call the Seek () function to locate the file pointer. The complete code containing the file positioning function is shown below:
C ++ code
1. // Create and write files to open files
2.CFile file;
3.file.Open ("C: //TestFile.txt", CFile :: modeWrite | CFile :: modeCreate);
4.
5.// Write to file
6.memset (WriteBuf, 'a', sizeof (WriteBuf));
7.file.SeekToBegin ();
8.file.Write (WriteBuf, sizeof (WriteBuf));
9.
10.// Close the file
11.file.Close ();
12.
13. // Open the file in read-only mode
14.file.Open ("C: //TestFile.txt", CFile :: modeRead);
15.while (true)
16. {
17. // file pointer
18.static int position = 0;
19.// move the file pointer
20.file.Seek (position, CFile :: begin);
21. // read file data
22.int ret = file.Read (ReadBuf, 100);
23.position + = ret;
twenty four.……
25.
26.// abort the loop if the end of the file is reached
27.if (ret <100)
28.break;
29.}
30.
31.// close the file
32.file.Close ();
Added:
Use the CFile class to read the file according to the structure, such as:
C ++ code
1.CFile fileRead, fileWrite;
2.fileRead.Open (_T ("E: //a.dat"), CFile :: modeRead); // Use the macro _T here
3.fileWrite.Open (_T ("E: //backup.txt"), CFile :: modeCreate | CFile :: modeWrite);
4.VIDEOHEADER * videoheader = new VIDEOHEADER ();
5.fileRead.Read (videoheader, sizeof (VIDEOHEADER));
6.char buf [sizeof (VIDEOHEADER) * 8];
7.sprintf (buf, "videoheader.cCommandID:% s, videoheader-> cCommandID); // Format the data we need to write to the file through sprintf, so the data stored in the file is in the format defined here to show.
8.fileWrite.Write (buf, strlen (buf));
Unless otherwise specified, chicken pecking articles are original
Reprinted please indicate the address of this article: http://www.jizhuomi.com/software/497.html
January 11, 2016