![](/img/trans.png)
[英]Error in recv()/send() (in Socket) or read()/write() (in CFile)
[英]CFile Error On 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嗎? 我只是從嵌入式開發開始。
我沒有在Windows CE上使用MFC,但是在普通Windows上,通常的習慣用法是
mFile.Open(tmp, CFile::modeCreate|CFile::modeWrite);
即嘗試添加“ CFile :: modeWrite”到構造函數。 MSDN文檔建議這樣做是必要的:
您可以使用按位或(|)運算符組合下面列出的選項。 需要一個訪問權限和一個共享選項; modeCreate和modeNoInherit模式是可選的。
對我而言,這意味着必須始終提供“ modeRead”或“ modeWrite”之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.