CFile class is used for opening and managing text and data files in applications or libraries written with the help of MFC framework. CFile class is basically a wrapper class over Win32 APIs like CreateFile, OpenFile, ReadFile, WriteFile and used to manage files in object oriented way. This can operate on a file handle which is already opened with Win32 CreateFile/OpenFile. However File path as string should be given to open a new file. This can be given in overloaded constructor CFile() or in Open() member call.
HANDLE hFile - Attach the handle of a file opened using CreateFile() or OpenFile() to the CFile object.
LPCTSTR lpszFileName - Relative path to the current working folder of the application or full path of a file to attach to the CFile object.
UINT nOpenFlags- Bitwise combination (OR) of file access options for the specified file. Discussed in detail in this article.
pTM - Pointer to CAtlTransactionManager object
nOpenFlags - A combination of one or more sharing and access mode can be given in this parameter. Specifies the action to take when opening the file. You can combine options listed below by using the bitwise-OR (|) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. The values are as follows:
- CFile::modeCreate - Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.
- CFile::modeNoTruncate - Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
- CFile::modeRead - Opens the file for reading only.
- CFile::modeReadWrite - Opens the file for reading and writing.
- CFile::modeWrite - Opens the file for writing only.
- CFile::modeNoInherit - Prevents the file from being inherited by child processes.
- CFile::shareDenyNone - Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.
- CFile::shareDenyRead - Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.
- CFile::shareDenyWrite - Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.
- CFile::shareExclusive - Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.
- CFile::shareCompat - This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.
- CFile::typeText - Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).
- CFile::typeBinary - Sets binary mode (used in derived classes only).
- CFile::osNoBuffer - See FILE_FLAG_NO_BUFFERING in CreateFile documentation.
- CFile::osWriteThrough - See FILE_FLAG_WRITE_THROUGH in CreateFile documentation.
- CFile::osRandomAccess - See FILE_FLAG_RANDOM_ACCESS in CreateFile documentation.
- CFile::osSequentialScan - See FILE_FLAG_SEQUENTIAL_SCAN in CreateFile documentation.
Opens a file from the name/path given.
These parameters are same as discussed earlier.
Nonzero if the open was successful; otherwise 0. The pError parameter is meaningful only if 0 is returned.
Reads data from file associated with the CFile object and put the data into a buffer.
VOID * lpBuffer - Pointer to the user-given buffer(should be non NULL). This API will retrieve the content from the filesystem and populate this buffer.
UINT nCount - The maximum number of bytes to be read from the file. For text-mode files, carriage return-line feed pairs are counted as single characters.
The number of bytes transferred to the buffer. For all CFile classes, the return value may be less than nCount if the end of file was reached.
Writes data to the file associated with the CFile object from the input buffer.
void * lpBuffer - A pointer to the user-given buffer that contains the data to be written to the file.
UINT nCount - The number of bytes to be transferred from the buffer. For text-mode files, carriage return-line feed pairs are counted as single characters.
None, Write throws an exception in response to several conditions, including the disk-full condition.
- Abort - Closes a file ignoring all warnings and errors.
- Close - Closes a file and frees the object.
- Duplicate - Constructs a duplicate object based on this file.
- Flush - Flushes any data yet to be written.
- GetFileName - Retrieves the filename of the selected file.
- GetFilePath - Retrieves the full file path of the selected file.
- GetFileTitle - Retrieves the title of the selected file.
- GetLength - Retrieves the length of the file.
- GetPosition - Retrieves the current file pointer.
- GetStatus - Retrieves the status of the open file, or in the static version, retrieves the status of the specified file (static, virtual function).
- LockRange - Locks a range of bytes in a file.
- Open - Safely opens a file with an error-testing option.
- Read - Reads (unbuffered) data from a file at the current file position.
- Remove - Deletes the specified file (static function).
- Rename - Renames the specified file (static function).
- Seek - Positions the current file pointer.
- SeekToBegin - Positions the current file pointer at the beginning of the file.
- SeekToEnd - Positions the current file pointer at the end of the file.
- SetFilePath - Sets the full file path of the selected file.
- SetLength - Changes the length of the file.
- SetStatus - Sets the status of the specified file (static, virtual function).
- UnlockRange - Unlocks a range of bytes in a file.
- Write - Writes (unbuffered) data in a file to the current file position.
This promram is a clone of DOS COPY command. It takes two arguments source file and destination file. Source file is opened with read flag and destication file is opened with write flag. It show how these open flags will be used. It copies the content of source file to destination file.
About our authors: Team EQA
MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.
This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...
Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.
DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.
LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.
We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...
Learn on Youtube