CStdioFile.open更改文件路径运行时出错?

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

如果我更改文件运行时错误的路径怎么办?

1.

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

上班

到另一个文件

2.

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

不工作 - 运行时错误?

要确定错误的原因,请使用引发 CFileException 的构造函数并使用 try/catch 块来处理该异常。

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() 显示系统错误信息。 TRACE 调用在调试输出中记录错误代码。您可以查找 error code in the reference 以获取更多信息。

请注意,不需要显式调用 CStdioFile::Close(),因为 CStdioFile 的析构函数会自动调用。

另外,建议始终使用绝对文件路径而不是相对路径。相对路径取决于当前目录,这通常不是您所期望的(不受您控制的代码可以随时更改)。