Original URL: http://blog.sina.com.cn/s/blog_623a7fa40100hh1u.html
CFile provides some common operating functions, as shown in table 1-2.
Table 1-2 CFile operator Functions |
Function |
Meaning |
Open |
Open File |
Close |
Close File |
Flush |
Refresh the data to be written |
Read |
Reading data from the current location |
Write |
Write data to the current location |
GetLength |
Get the size of a file |
Seek |
Position the file pointer to the specified location |
Seektobegin |
Position file pointer to file header |
Seektoend |
Locate the file pointer to the end of the file |
GetFileName |
Gets the file name, such as:"NOTEPAD. EXE " |
GetFilePath |
Gets the file path, such as:"C:\WINDOWS \notepad. EXE " |
GetFileTitle |
Get the file title, such as:"NOTEPAD" |
GetPosition |
Gets the current file pointer |
GetStatus |
Gets the status of the current file, returning a cfilestatus |
#Remove |
static method, delete the specified file |
#Rename |
static method, renaming the specified file |
Note that the last two static functions, in fact, encapsulate the functions of the Windows API for file management.
the process for manipulating files using CFile is as follows:
Constructs a CFile object.
Call the CFile::Open () function to create and open the specified file.
Call Cfile::read () and cfile::write () for file operations.
Call CFile::Close () to close the file handle.
The location settings for the file pointer can be used:
Seek ( LONG loff, UINT nfrom ) Move the file pointer to the specified position
Loff : Is the pointer offset byte number, or positive if the backward offset, or negative if the forward offset.
Nfrom : There are three types of values on MSDN:
Cfile::begin starting from the beginning of the file,Loff is a positive number;
Cfile::current the current position begins;
Cfile::end starting from the end of the file, theloff is negative;
Seektobegin ( ) Move the file pointer to the beginning of the file
seektoend ( ) Move the file pointer to the end of the file
GetPosition () Returns the position of the current file pointer
Gets the number of bytes of the file available getlength ( ) The return value of this function is DWORD, but it can be used directly to allocate the number of array elements, for example:
DOWRD Len=file. GetLength ();
Char *pbuf=new char[len+1]/int *pbuf=new INT[LEN/4]
Char occupies one byte, andint accounts for four bytes.
Write file:
CFile file;
File. Open ("E:\\vc\\1.txt", cfile::modecreate| cfile::modewrite| Cfile::modenotruncate,null);
File. Write ("HelloWorld", strlen ("HelloWorld"));
Write ( const void* lpbuf, UINT ncount ) lpbuf is the Buf pointer that writes the data ,ncount is the number of bytes in the Buf that need to be written to the file
File.close ();
Read file:
CFile file;
File. Open ("E:\\vc\\1.txt", cfile::moderead,null);
DWORD Len=file. GetLength ();
Char buf[len+1];
buf[len]=0; 0 terminating string for output.
File. Read (Buf,len); Read ( void* lpbuf, UINT ncount ) lpbuf Is the Buf pointer that is used to receive the read data ncount is the number of bytes read from the file
MessageBox (BUF);
"Go" MFC Read and write files with CFile 2