CFile异常:CFileException::fileNotFound

ray1989716 2015-07-29 03:20:50
TRY{
File.Open(FilePath,CFile::modeCreate|CFile::modeWrite);
File.Write(Data,DataLen);
File.Flush();
File.Close();
return;
}
CATCH(CFileException,e )
{
.......
}END_CATCH

确保FilePath路径正确,IO操作很频繁,会出现CFileException::fileNotFound这种异常,想知道为什么?
有没有好的方法可以替代~
...全文
513 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ray1989716 2015-07-29
  • 打赏
  • 举报
回复
引用 3 楼 numen27 的回复:
这个用法不对吧,MSDN文档CFile::Open上这么说的: If you don't supply the pError parameter, or if you pass NULL for pError, Open will return FALSE and not throw a CFileException. 就是你这种用法,Open函数应该不会抛出CFileException吧。 应该这么用: CFile file; CFileException ex; if (!file.Open( FilePath,CFile::modeCreate|CFile::modeWrite, &ex)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); cout << "Open file "<<FilePath<<" Failed:"<<szError; }
刚刚测试过你说的,的确如此。不提供CFileException* pError 这个参数或者为NULL,异常将被忽略。所以我之前抓的异常并不是Open报的,不能反映Open失败的原因,谢谢。
numen27 2015-07-29
  • 打赏
  • 举报
回复
这个用法不对吧,MSDN文档CFile::Open上这么说的: If you don't supply the pError parameter, or if you pass NULL for pError, Open will return FALSE and not throw a CFileException. 就是你这种用法,Open函数应该不会抛出CFileException吧。 应该这么用: CFile file; CFileException ex; if (!file.Open( FilePath,CFile::modeCreate|CFile::modeWrite, &ex)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); cout << "Open file "<<FilePath<<" Failed:"<<szError; }
ray1989716 2015-07-29
  • 打赏
  • 举报
回复
引用 1 楼 xiaoc1026 的回复:
File.Open 都不判断返回值就往下执行?
你的意思是Open失败了吧,ok,对返回值加上判断,另外程序中我会尝试4次,联系4次都是同样的CFileException::fileNotFound,为什么会出现这样的现象呢?有办法优化吗?谢谢。
见习学术士 2015-07-29
  • 打赏
  • 举报
回复
File.Open 都不判断返回值就往下执行?
// 文件合并涵数 int CFileSpltDlg::MergeMe() { CWaitCursor wait; // constructing these file objects CFile destFile; // we'll use a CFileException object to get error information CFileException ex; BYTE buffer[140000]; DWORD dwRead; UINT nCount = 140000; UINT newlen = 1400000; char buff [20]; long l = 1; CString name; CString pref; CString newpath; UpdateData(TRUE); //open file for read if (!m_path.IsEmpty()) { if (!m_SourceFile.Open(m_path, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); m_edit.SetFocus(); m_edit.SetSel(0, -1); return 1; } //construct new name m_filename = m_path.Right((m_path.GetLength() - m_path.ReverseFind('\\')) - 1); //close file m_SourceFile.Close(); } //constuct a new path name newpath = m_path.Left(m_path.GetLength() - m_filename.GetLength()); if (!m_targetpath.IsEmpty()) { //some silly check, that could be chnged if (!m_filename.IsEmpty() && m_filename.Left(2) != _T("1_")) { ::AfxMessageBox(_T("待合并的源文件名不对..."), MB_ICONERROR); return 1; } else if(m_filename.IsEmpty()) { MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } //constuct an original file name m_filename = m_filename.Right(m_filename.GetLength() - 2); //判断选择目录未尾是否已有"\"符 if(m_targetpath.Right(1)=='\\') m_path = m_targetpath + m_filename; else m_path = m_targetpath + _T("\\") + m_filename; //create target file if (!destFile.Open(m_path, CFile::modeWrite | CFile::shareExclusive | CFile::typeBinary | CFile::modeCreate, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); return 0; } } else if(m_path.IsEmpty()) {//souce is not there MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } if (m_targetpath.IsEmpty()) {//target is not there MessageBox(_T("请选择合并后要保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } //do merge do { //constuct a new name by dynamicly incrementing prefix pref = _ltoa(l, buff, 10); pref += _T("_"); //open file with new name if (!m_SourceFile.Open(newpath + pref + m_filename, CFile::modeRead | CFile::shareExclusive | CFile::typeBinary, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); destFile.Close(); m_path = _T(""); m_filename = _T(""); // pProgress.SetPos(0); newpath = _T(""); // m_parts = _T(""); UpdateData(FALSE); //return OK because this f_n is aborting the loop if name is not found return 0; } else //constuct a new name name = _T(newpath + pref + m_filename); do {//write into file while it size < than 1.4 MB dwRead = m_SourceFile.Read(buffer, nCount); destFile.Write(buffer, dwRead); } //while we can read from source file while (dwRead > 0); m_SourceFile.Close(); // Set the range to be 0 to 500. pProgress.SetRange(0, 500); // Set the position for (int i = 0; i < 500; i++) pProgress.SetPos(i); m_parts = _ltoa(l, buff, 10); m_parts += _T("个文件已合并"); UpdateData(FALSE); l++; UpdateWindow(); } while (l < 500);//little bit dirty solution, but you can always improve it!... return 0; } //文件分割涵数 int CFileSpltDlg::SplitMe() { CWaitCursor wait; // constructing these file objects CFile destFile; // we'll use a CFileException object to get error information CFileException ex; DWORD dwRead; UINT newlen; char buff [20]; char b [20]; long l = 1; CString name; UINT len = 0; // CGradientProgressCtrl *pProgress = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS); UpdateData(TRUE); //获取文件分割后的大小,定义相对应变量数值 newlen=GetSplitFileSize(); UINT nCount = newlen/10; BYTE buffer[140000]; //open file for read //m_path contain the file path if (!m_path.IsEmpty()) { if (!m_SourceFile.Open(m_path, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); m_edit.SetFocus(); m_edit.SetSel(0, -1); return 1; } //get file length len = m_SourceFile.GetLength(); } //too lazy to put all "hard coded" strings in string table else { MessageBox(_T("请选择待分割的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } if (m_targetpath.IsEmpty()) { MessageBox(_T("请选择分割后保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } //quick and dirty check for file size if (len < newlen) { CString length = _itoa(len, b, 10); MessageBox(_T("文件长度为 " + length + " 字节,不够指定的分割大小, 没有必要再进行分割."), _T("文件分割器"), MB_ICONEXCLAMATION); m_SourceFile.Close(); m_path = _T(""); m_filename = _T(""); UpdateData(FALSE); return 1; } //do split do { //constuct a new name dynamicly changing prefix name = _ltoa(l, buff, 10); name += _T("_"); CString newpath; //判断选择目录未尾是否已有"\"符 if(m_targetpath.Right(1)=='\\') newpath = m_targetpath; else newpath = m_targetpath + _T("\\"); if (!destFile.Open(newpath + name + m_SourceFile.GetFileName(), CFile::modeWrite | CFile::shareExclusive | CFile::typeBinary | CFile::modeCreate, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); m_SourceFile.Close(); return 1; } do { dwRead = m_SourceFile.Read(buffer, nCount); destFile.Write(buffer, dwRead); }//while size is less than 1.4 MB while (dwRead > 0 && destFile.GetLength() < newlen); destFile.Close(); // Set the range pProgress.SetRange(0, len /newlen*10); // Set the position pProgress.SetPos(l); m_parts = _ltoa(l , buff, 10); m_parts += _T("个文件生成"); UpdateData(FALSE); l++; UpdateWindow(); } while (dwRead > 0); // close source m_SourceFile.Close(); m_path = _T(""); m_filename = _T(""); // pProgress.SetPos(0); // m_parts = _T(""); UpdateData(FALSE); return 0; }

16,490

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧