CFile::Open

Syntax

virtual BOOL Open( const char* pszFileName, UINT wStyleFlags, CFileException* pError = NULL );

Parameters

pszFileName

A string that is the path to the desired file. The path may be relative or absolute, but may not contain a network name.

wStyleFlags

A WORD that defines the file's sharing and access mode. It specifies the action to take when opening the file. You can combine options by using the bitwise-OR (|) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. See the CFile constructor for a list of mode options.

pError

A pointer to an existing file-exception object that indicates the completion status of the open operation.

Remarks

Open is designed for use with the default CFile constructor. The two functions form a “safe” method for opening a file where a failure is a normal, expected condition. The constructor is guaranteed to succeed, and Open returns (a pointer to) an exception object, bypassing the THROW/TRY/CATCH mechanism. Thus there is no possibility of a memory leak due to a failing constructor.

Return Value

TRUE if the open was successful; otherwise FALSE. The pError parameter is only meaningful if FALSE is returned.

Example

CFile f;

CFileException e;

char* pFileName "test.dat";

if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )

{

#ifdef _DEBUG

afxDump << "File could not be opened " << e.m_cause << "\\n";

#endif

}

See Also

CFile::CFile, CFile::Close