Your call seems to be correct, but there are a lot of possible variables. Are you certain there's space left? Are you certain the file is not locked when you attempt to open it? Below is code that I've used to successfully open and append to a file on the FlashFX disk without any problem.
#include "windows.h"
#define FILE_SIZE 0x4000
const TCHAR *ROOT_DIRECTORY = _T("FlashFX Disk");
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
ULARGE_INTEGER freeSpace;
ULARGE_INTEGER totalSpace;
long elapsed_time;
HANDLE hFile;
BYTE *outBuffer;
DWORD bytesWritten;
long lret;
int i;
unsigned char *p;
TCHAR filename[256];
TCHAR rootname[256];
_stprintf(rootname, ROOT_DIRECTORY);
RETAILMSG(TRUE, (_T("--- Begin File System Testing in %s ---\r\n"), rootname));
GetDiskFreeSpaceEx(rootname, &freeSpace, &totalSpace, NULL);
RETAILMSG(TRUE, (_T(" %i bytes free\r\n"), freeSpace));
RETAILMSG(TRUE, (_T(" Using %i block/file size\r\n"), FILE_SIZE));
RETAILMSG(TRUE, (_T(" Writing %i blocks/files\r\n"), freeSpace.QuadPart / FILE_SIZE));
iterations = (short)(freeSpace.QuadPart / FILE_SIZE);
RETAILMSG(TRUE, (_T(" Iterations: %i\r\n"), iterations));
// populate write buffer
p = outBuffer = (BYTE *)LocalAlloc(LPTR, FILE_SIZE);
for(i = 0 ; i < FILE_SIZE ; i++)
{
memcpy(p, &i + (sizeof(i) - 1), 1);
p++;
}
RETAILMSG(TRUE, (_T("--- Beginning Contiguous Memory Speed Test ---\r\n")));
RETAILMSG(TRUE, (_T("Writing %i "), iterations));
RETAILMSG(TRUE, (_T("%i byte blocks\r\n"), FILE_SIZE));
_stprintf(filename, _T("%s\\Flash.tst"), rootname);
hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile == INVALID_HANDLE_VALUE)
{
RETAILMSG(TRUE, (_T("\r\nFAILED TO CREATE FILE! GetLastError Returned %i\r\n"), GetLastError()));
return 0;
}
for(i = 0 ; i < iterations ; i++)
{
elapsed_time = GetTickCount();
lret = WriteFile(hFile, outBuffer, FILE_SIZE, &bytesWritten, NULL);
if((bytesWritten != FILE_SIZE) || (lret == 0))
{
RETAILMSG(TRUE, (_T("\r\nFAILED TO WRITE TO FILE! GetLastError Returned %i\r\n"), GetLastError()));
break;
}
elapsed_time = GetTickCount() - elapsed_time;
RETAILMSG(TRUE, (_T("%i\r\n"), elapsed_time));
}
CloseHandle(hFile);
RETAILMSG(TRUE, (_T("Deleting file...\r\n")));
DeleteFile(filename);
RETAILMSG(TRUE, (_T("--- Finished Contiguous Memory Speed Test ---\r\n")));
// free buffer
LocalFree(outBuffer);
RETAILMSG(TRUE, (_T("--- Persistent Storage Testing Done---\r\n")));
return 0;
}