CFile();
CFile( int hFile );
CFile( const char* pszFileName, UINT wOpenFlags )
throw( CFileException );
hFile
The handle of a file that is already open.
pszFileName
A string that is the path to the desired file. The path may be relative or absolute.
wOpenFlags
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.
Value | Meaning |
CFile::modeCreate | Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length. |
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 | Opens the file with compatibility mode, allowing any process on a given machine to open the file any number of times. Construction fails if the file has been opened with any of the other sharing modes. |
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). |
The default constructor does not open a file, but rather sets m_hFile to CFile::hFileNull. Because this constructor does not throw an exception, it does not make sense to use TRY/CATCH logic. Use the Open member function, then test directly for exception conditions. For a discussion of exception processing strategy, see the cookbook in the Class Libraries User's Guide.
The constructor with one argument creates a CFile object that corresponds to an existing operating-system file identified by hFile. No check is made on the access mode or file type.
The constructor with two arguments creates a CFile object and opens the corresponding operating-system file with the given path. This constructor combines the functions of the first constructor and the Open member function. It throws an exception if there is an error while opening the file. Generally this means that the error is unrecoverable and that the user should be alerted.
char* pFileName = "test.dat";
TRY
{
CFile f( pFileName, CFile::modeCreate | CFile::modeWrite );
}
CATCH( CFileException, e )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e->m_cause << "\\n";
#endif
}