Before opening a file, it’s better to confirm if it exists.
|
WIN32_FIND_DATA fd; HANDLE hFile = ::FindFirstFile( fName, &fd );
if ( hFile == INVALID_HANDLE_VALUE ) { // file must not exist -- warn user
CString sMessage; sMessage.Format( "Could not find specified file/n/"%s/"", fName );
::MessageBox( NULL, sMessage, "Could Not Find File", MB_OK | MB_ICONEXCLAMATION );
::FindClose( hFile ); return;
}
::FindClose( hFile ); |
本文介绍了一个简单的Win32 API示例,用于在打开文件前检查该文件是否存在于指定路径。通过使用FindFirstFile函数,可以有效地避免因文件不存在而导致的应用程序错误。
755

被折叠的 条评论
为什么被折叠?



