我正在使用Windows CE 4.2和MS Embedded VC ++ 4.0。下面的代码给出了错误访问[文件名]被拒绝。,它创建了文件,但没有写任何内容。

CString tmp;
tmp.Format(_T("%s%d"), mFileName, ++ctr);
TRY 
{
  mFile.Open(tmp, CFile::modeCreate);
  mFile.Write(&data[ctr%2], 1);
  mFile.Close();
}
CATCH (CException, e)
{
  TCHAR szCause[255];
  CString strFormatted;

  e->GetErrorMessage(szCause, 255);
  strFormatted += szCause;
  AfxMessageBox(strFormatted);
}
END_CATCH

有趣的是,使用 CreateFile 可以正常工作:

CString tmp;
tmp.Format(_T("%s%d"), mFileName, ++ctr);

hFile = CreateFile(tmp, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL /*| FILE_FLAG_WRITE_THROUGH*/, 0);
WriteFile(hFile, &(data[ctr%2]), 1, &bytesWritten, NULL);
CloseHandle(hFile);

为什么会这样?我甚至可以在WinCE上使用CFile吗?我刚刚开始使用嵌入式开发。

有帮助吗?

解决方案

我没有在Windows CE上使用MFC,但在普通的Windows上,通常的习惯用语是

  mFile.Open(tmp, CFile::modeCreate|CFile::modeWrite);

即。尝试添加“CFile :: modeWrite”到构造函数。 MSDN文档表明这是必要的:

您可以使用按位OR(|)运算符组合下面列出的选项。需要一个访问权限和一个共享选项; modeCreate和modeNoInherit模式是可选的。

这对我来说意味着“modeRead”之一。或“modeWrite”必须始终提供。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top