The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Mr Earl on April 24, 2005, 11:14:27 AM

Title: WriteFile File Size problem
Post by: Mr Earl on April 24, 2005, 11:14:27 AM
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?
Title: Re: WriteFile File Size problem
Post by: pbrennick on April 24, 2005, 11:56:23 AM
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
Title: Re: WriteFile File Size problem
Post by: Mr Earl on April 24, 2005, 12:56:27 PM
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?
Title: Re: WriteFile File Size problem
Post by: thomasantony on April 24, 2005, 03:04:55 PM
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
Title: Re: WriteFile File Size problem
Post by: Mr Earl on April 24, 2005, 04:13:36 PM
Thank you Thomas!  Problem solved.
Title: Re: WriteFile File Size problem
Post by: raymond on April 25, 2005, 03:20:33 AM
There is no need to use the SetFilePointer after the WriteFile operation. You can simply use SetEndOfFile before closing the file.

Raymond