I'm writing a series of bytes from the buffer to a file using WriteFile. After then I do some operation and again write the next byte from the buffer to the file. This time I understand the byte overwrites the previously written byte so I put in SetFilePointer to move the pointer to the next place...

But I could see that it still overwrites the same byte that is written previously. Ultimately I'm only writing the last byte of the buffer to the file. Pls can you point the mistake.

FilePointer is declared as DWORD and is initialized to 0

Buffer db 1,2,3



invoke CreateFile, ADDR lpFileName, GENERIC_READ+GENERIC_WRITE, FILE_SHARE_WRITE,\
                  NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
mov hFile, eax
xor esi, esi

.WHILE TRUE

   .IF eax != NULL
       invoke WriteFile, hFile, ADDR Buffer, FilePointer, ADDR lpNumWritten, ADDR lpOverlapped
        ::::::::Some Operation:::::::::::
       invoke SetFilePointer, hFile, FilePointer, 0, FILE_CURRENT
       
   .ENDIF
   
   inc esi
   inc FilePointer    
   
   .IF esi > 18
       invoke ExitProcess, NULL
       
   .ENDIF

.ENDW

Posted on 2009-09-17 09:55:09 by karthikeyanck
"ADDR lpOverlapped" has no place in there, you didn't open the file with overlapped-flag.
WriteFile will auto-increment for you, when not overlapped.
Doing ExitProcess before closing handles is harsh.
"if eax!=NULL" - you realize that every call in that proc of yours overwrites EAX, right?


Here's what happens in your call to WriteFile
"If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile does not return until the write operation has been completed. "
Posted on 2009-09-17 11:36:51 by Ultrano
It would be better to store all the bytes into a buffer and then write the buffer to the file only once.  Write operations can take a while and should be minimized if possible espeically on NAND (USB memory, etc).  You won't need to set file pointer either unless you wish to append to an existing file.
Posted on 2009-09-17 15:51:19 by GoldStar611
Also, when writing 'chunks' of data, check to see how much data actually got written - don't assume that the number of bytes written is what you requested. It usually will be, but not always.

Posted on 2009-09-17 23:10:14 by Homer
Thanks guys for the replies, works perfect  8)
Posted on 2009-09-18 10:48:15 by karthikeyanck