vc操作常用文件操作(三) CFile Example

CFile is the class used for handling Files in MFC. This class can be used for creating, reading, writing and modifying files. It directly provides unbuffered, binary disk input/output services, and it indirectly supports text files and memory files through its derived classes.

CFile - Creating a File:

   There are two ways of creating files. One way is to instantiate the CFile object with the file path. This creates the file. The second way is to call the Open function. This also creates the file.c:\\test\\codersource_cfile_example.txt", CFile::modeCreate|CFile:: modeReadWrite);c:\\test\\codersource_cfile_example.txt", CFile::modeCreate|CFile:: modeReadWrite);

   If the file is opened without specifying the mode constant

CFile - Writing to a File:

   The function Write is used to write data to the files. The sample code is as follows.

   If there is any need to write text line by line, it is better to use the class CStdioFile.

CFile - Reading from a file:

   The function Read is used to read data from files. The sample code is,

   The function returns the number of bytes read from the file. The maximum number of characters read will be the second parameter of the Read function.

CFile - closing the file:

   The Close function is used to close the file. But the close function need not be called, as the destructor will automatically call it if the file is open. So when the object goes out of scope, the destructor calls close function.

from: http://www.codersource.net/mfc_cfile.html

posted @ 2010-12-03 22:57  BuildNewApp  阅读(341)  评论(0编辑  收藏  举报