CFile打开文件失败 sharingViolation
程序大致是这样的,程序中不停地调用这个函数,每调用一次即往文件里写一次数据。大概写个2-3万次后,会出面一次文件打开失败。其中文件打开异常值m_cause为CFileException::sharingViolation。程序是顺序执行的,没有在其他函数中使用到此文件。也没有在其他地方打开此文件。随机出现,大概写个几万条数据,就会出现一次打开文件失败。不知何故?
void fun()
{
CStdioFile file;
CFileException e;
if(!file.Open(filePath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::typeBinary))
{
Sleep(100);
if(!file.Open(filePath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::typeBinary,&e))
{
str.Format(_T("打开文件失败,异常代码:%d"),e.m_cause);
AfxMessageBox(str,MB_ICONSTOP);
exit(0);
return;
}
}
……
file.Abort(); //关闭文件
}