CFile::CFile 

CFile( );

CFile( int hFile );

CFile( LPCTSTR lpszFileName, UINT nOpenFlags );
throw( CFileException );

Parameters

hFile

The handle of a file that is already open.

lpszFileName

A string that is the path to the desired file. The path can be relative or absolute.

nOpenFlags

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:

Remarks

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 article Exceptions in Visual C++ Programmer'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. When the CFile object is destroyed, the operating-system file will not be closed. You must close the file yourself.

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.

Example

//example for CFile::CFile
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
}
END_CATCH

CFile OverviewClass MembersHierarchy Chart