views:

303

answers:

1

I am using Windows CE 4.2 and MS Embedded VC++ 4.0. The following code gives me the error 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:

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? I'm just starting out with embedded development.

+1  A: 
DavidK
Thanks, what a boneheaded mistake!
Nick