silver
silver

Reputation: 41

CStdioFile.open Change the path to the file Runtime error?

What if I change the path to the file Runtime error?

1.

CStdioFile file;    
file.Open(_T("‏‏hb_n.txt"), CFile::modeRead | CFile::typeUnicode);
file.Close();

to work

To another file

2.

CStdioFile file;    
file.Open(_T("‏‏hb_n_2.txt"), CFile::modeRead | CFile::typeUnicode);
file.Close();

Not working - Runtime error?

Upvotes: 0

Views: 383

Answers (1)

zett42
zett42

Reputation: 27766

To determine the cause of the error, use the constructor that throws a CFileException and use a try/catch block to handle that exception.

try
{
    CStdioFile file( _T("hb_n_2.txt"), CFile::modeRead | CFile::typeUnicode );
}
catch( CFileException* e )
{
    TRACE( L"Error code: %d\n", e->m_lOsError );
    e->ReportError();
    e->Delete();
}

CFileException::ReportError() shows the system error message. The TRACE call logs the error code in the debug output. You can lookup the error code in the reference to get additional information.

Note that it is not required to explicitly call CStdioFile::Close() as the destructor of CStdioFile will do it automatically.

Also it is recommended to always use absolute file paths instead of relative paths. Relative paths depend on the current directory which often is not what you expect (code that is not under your control could change it at any time).

Upvotes: 1

Related Questions