简体   繁体   English

Windows Ce上的CFile错误

[英]CFile Error On Windows Ce

I am using Windows CE 4.2 and MS Embedded VC++ 4.0. 我正在使用Windows CE 4.2和MS Embedded VC ++ 4.0。 The following code gives me the error Access to [file name] was denied. 以下代码给我错误: Access to [file name] was denied. , and it creates the file but does not write anything to it. ,它会创建文件,但不会对其写入任何内容。

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

Interestingly, using CreateFile works fine: 有趣的是,使用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);

Why could this be? 为什么会这样呢? Can I even use CFile on WinCE? 我甚至可以在WinCE上使用CFile吗? I'm just starting out with embedded development. 我只是从嵌入式开发开始。

I've not used MFC on Windows CE, but on ordinary Windows, the usual idiom is 我没有在Windows CE上使用MFC,但是在普通Windows上,通常的习惯用法是

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

ie try adding "CFile::modeWrite" to the constructor. 即尝试添加“ CFile :: modeWrite”到构造函数。 The MSDN documentation suggests that this is necessary: MSDN文档建议这样做是必要的:

You can combine options listed below by using the bitwise-OR (|) operator. 您可以使用按位或(|)运算符组合下面列出的选项。 One access permission and one share option are required; 需要一个访问权限和一个共享选项; the modeCreate and modeNoInherit modes are optional. modeCreate和modeNoInherit模式是可选的。

This implies to me that one of "modeRead" or "modeWrite" must always be supplied. 对我而言,这意味着必须始终提供“ modeRead”或“ modeWrite”之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM