News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

WriteFile File Size problem

Started by Mr Earl, April 24, 2005, 11:14:27 AM

Previous topic - Next topic

Mr Earl

I have a file with bad data at the beginning.  To correct it I shifted the good data up over the bad data.
I changed the File Size, reset the File Pointer and re-wrote the file using the smaller File Size.
The problem is that when I examined the output, the bad data was correctly shifted out, but the File Size
was not changed, and I had the same sized file, now with garbage at the end.
What did I do wrong, or what else do I have to do to get the File Size changed?

pbrennick

Depending on the type of database, the answer can vary.  I think the amount of records is being stored in there somewhere.  This would cause that exact effect.  If the database is not too large and is not propietory, I will look at it for you.  BTW: a lot of database handlers offer a way to rebuild themselves.

Paul

Mr Earl

Paul,  I'm not using a database, this is a simple 1-record File using the API invokes for heap,open,read,write, etc.  In this program the File Size and the Record Size are the same, since it is a 1-record File.  The problem is not a record count problem, but a byte count for the file size.  At other times I have had to INCREASE the file size (record size) and the Size was increased correctly.
Simply put, this is the command that isn't working right:

   mov FILESIZE,ecx
   INVOKE WriteFile,hFile,\
               pMemory,\
               FILESIZE,\
               ADDR SizeReadWrite,\
               NULL

The FILESIZE that I specified is NOT applied to the output file.  The size remains the same as it was before I re-wrote File.  Maybe you can't DECREASE the File Size unless you re-create it?

thomasantony

Hi,
  Use SetFilePointer with the filesize you want and then use SetEndOfFile .
DWORD SetFilePointer(
    HANDLE hFile,   // handle of file
    LONG lDistanceToMove,   // number of bytes to move file pointer
    PLONG lpDistanceToMoveHigh,   // address of high-order word of distance to move 
    DWORD dwMoveMethod    // how to move
   );

BOOL SetEndOfFile(
    HANDLE hFile    // handle of file whose EOF is to be set
   );

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

Mr Earl

Thank you Thomas!  Problem solved.

raymond

There is no need to use the SetFilePointer after the WriteFile operation. You can simply use SetEndOfFile before closing the file.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com