Q:

Using WriteFile to fill up a cluster

I want to use Writefile to fill up then end of every file until it reaches the end of its last cluster. Then I want to delete what I wrote and repeat the process(attempting to get rid data that might have been there).
 I have a 2 issues:

WriteFile gives me an error: ERROR_INVALID_PARAMETER 
Depending on the type of file, WriteFile() gives me different results

So for the first issue I realized that the parameter nNumberOfBytesToWrite in the WriteFile() has to be a multiple of bytes per sector(my case is 512 bytes). Is this a limitation of the function or am I doing something wrong? 
In my second issue, I'm using two dummy files(.txt and .html) on an external hard drive to write random data to. In the case of the .txt file, the data is written to the end of the file which is what I need. However, the .html file just writes to the beginning of the file and replaces any data that was already there.
 Here are some code snippets relevant to my issue:
hFile = CreateFile(result,          
                   GENERIC_READ | GENERIC_WRITE |FILE_READ_ATTRIBUTES,  
                   FILE_SHARE_READ | FILE_SHARE_WRITE,
                   0,                           
                   OPEN_EXISTING,                    
                   FILE_FLAG_NO_BUFFERING,
                   0);                          

if (hFile == INVALID_HANDLE_VALUE)  {
        cout << "File does not exist" << endl;
        CloseHandle(hFile);
}

DWORD dwBytesWritten;
char * wfileBuff = new char[512];
memset (wfileBuff,'0',512);

returnz = SetFilePointer(hFile, 0,NULL,FILE_END);
    if(returnz ==0){
        cout<<"Error: "<<GetLastError()<<endl;
};

LockFile(hFile, returnz, 0, 512, 0) 

returnz =WriteFile(hFile, wfileBuff, 512, &dwBytesWritten, NULL);
if(returnz ==0){
            cout<<"Error: "<<GetLastError()<<endl;
}

UnlockFile(hFile, returnz, 0, 512, 0);

cout<<dwBytesWritten<<endl<<endl;

I am using static numbers at the moment just to test out the functions. Is there anyway I can always write to the the end of the file no matter what type of file? I also tried SetFilePointer(hFile, 0,(fileSize - slackSpace + 1),FILE_BEGIN); but that didn't work.

A:

You need to heed the information in the documentation concerning FILE_FLAG_NO_BUFFERING. Specifically this section:

As previously discussed, an application must meet certain requirements
  when working with files opened with FILE_FLAG_NO_BUFFERING. The
  following specifics apply:

File access sizes, including the optional file offset in the OVERLAPPED structure, if specified, must be for a number of bytes that
  is an integer multiple of the volume sector size. For example, if the
  sector size is 512 bytes, an application can request reads and writes
  of 512, 1,024, 1,536, or 2,048 bytes, but not of 335, 981, or 7,171
  bytes.
File access buffer addresses for read and write operations should be physical sector-aligned, which means aligned on addresses in memory
  that are integer multiples of the volume's physical sector size.
  Depending on the disk, this requirement may not be enforced.

