CFile class is used for opening a file.
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.
Sharing and access mode. 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:
Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.
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.
Opens the file for reading only.
Opens the file for reading and writing.
Opens the file for writing only.
Prevents the file from being inherited by child processes.
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.
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.
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.
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.
This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.
Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).
Sets binary mode (used in derived classes only).
See FILE_FLAG_NO_BUFFERING in CreateFile in the Platform SDK.
See FILE_FLAG_WRITE_THROUGH in CreateFile in the Platform SDK.
See FILE_FLAG_RANDOM_ACCESS in CreateFile in the Platform SDK.
See FILE_FLAG_SEQUENTIAL_SCAN in CreateFile in the Platform SDK.
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.
/* mycopy.cpp This console program uses CFile to copy binary files. */
/* syntax: mycopy
int main(int argc, char *argv)
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.