With The Case Of  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
XD++ Library
TFC Library
Free Products
Technical Support

Get Ready to Unleash the Power of UCanCode .NET

UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"

MFC Example: CFile and Creating a File, Open, Write, CFile::modeCreate, CFile:: modeReadWrite

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.

     CFile cfile_object( "c:\\test\\ucancode_cfile_example.txt", CFile::modeCreate|CFile:: modeReadWrite);

     CFile cfile_object;
     cfile_object.Open( "c:\\test\\
",  CFile::modeCreate|CFile:: modeReadWrite);
   The first parameter to both the functions (CFile() constructor and Open()) is the physical path of the file in the disk. The second parameter is an enumerated constant. This specifies the mode of opening the file object. The above constants modeCreate implies "create a new file" and modeReadWrite means "open the file for both reading and writing".

   If the file is opened without specifying the mode constant shareDenyNone, this file can be opened in read mode by other programs. This feature will be necessary for text files, logs created by programs. For creating text files we use CFile::typeText and for binary files CFile::typeBinary.

CFile - Writing to a File:

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

     CFile cfile_object;
     cfile_object.Open( "c:\\test\\
ucancode_cfile_example.txt", CFile::modeCreate|CFile::modeWrite);

     char szSampleText[100];
     strcpy(szSampleText, "Sample Text for CFile Write function Example");
     cfile_object.Write (szSampleText,100); 

   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,

     CFile cfile_object;
     cfile_object.Open( "c:\\test\\
_cfile_example.txt", CFile::modeCreate|CFile::modeWrite);

     char szSampleText[100];
     UINT lBytesRead = cfile_object.Read (szSampleText,100); 

   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.

Full VC++ Source Codes, Tutorial and Example



Copyright ?1998-2007 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to