Windows Ce上的CFile错误

时间:2008-10-29 12:57:07

标签: c++ file-io windows-ce

我正在使用Windows CE 4.2和MS Embedded VC ++ 4.0。下面的代码给出了错误Access to [file name] was denied.,它创建了文件,但没有写任何内容。

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吗?我刚刚开始使用嵌入式开发。

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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