Changing File Size


On usual file activity, its size can merely increase whenever new data is written (appended) at the end of file. This operation is transparent to the user and is performed automatically by the operating system.

Developers have also more sophisticated methods for adding or deleting data anywhere in the file. With Expand method you can insert data block of specified size at the specified position. File is expanded by the number of bytes being inserted and overlapping data is moved forward to make room for new data block. Similarly, Shrink can be used to delete block of data from file. Overlapping data is moved backward and file is shrinked to remove junk data. Both methods return new size of the file on completion.

To resize file manually use SetSize method, which sets size of the file to the absolute value within range 0 to 2GB. File is always resized from the end; when increased, empty block of data is appended at the end of file; when decreased, data is cut off from the end of file. In any case specified size always determines physical size of the file - on successfull completion size of the file is exactly as specified. All methods that change size of the file always flush unsaved data present in internal buffer prior to changing file size.

To retrieve current size of the file call GetSize method; returned size, however, can be greater than actual physical file size because it specifies size the file will have when all unsaved data is flushed. In other words it specifies current physical size of the file increased by the size of written data that will expand file.

Examples

C++

// error handling is omitted for clarity


  int main(int, char**)
  {
   IFile ixf;
   long newsize;
   BYTE data[1024];

   ixf.SetLicenseKey("YOURLICENSEKEY");

// initialize and open (create) file for reading/writing

   ixf.Initialize(); 
   ixf.Open("test.bin", TRUE); 

// check file size
// and resize to 1KB if necessary

   if(ixf.GetSize() != 1024)
    ixf.SetSize(1024);

// write 1024 'X' characters
// starting from beginning of file

   memset(data, (int)'X', sizeof(data));
   ixf.PutByte(data, 1024, 0);

// insert block of 256 bytes in the middle of the file
// on completion 'newsize' should be 1280

   newsize = ixf.Expand(512, 256);

// write 256 'O' characters into new block

   memset(data, (int)'O', sizeof(data));
   ixf.PutByte(data, 256, 512);

// finally remove inserted block
// on completion 'newsize' should be again 1024
// and file should contain only 'X' characters

   newsize = ixf.Shrink(512, 256);

    .
    .
    .

   return(0);

  }

BASIC

' error handling is omitted for clarity


  Sub Main()

   Dim ixf As IXFile
   Dim newsize as Long
   Dim data(1023) As Byte
   Dim i As Integer

   Set ixf = New IXFile
   ixf.SetLicenseKey "YOURLICENSEKEY"

' initialize and open (create) file for reading/writing

   ixf.Initialize 
   ixf.Open "test.bin", TRUE 

' check file size
' and resize to 1KB if necessary

   If ixf.GetSize() <> 1024 Then
    ixf.SetSize 1024
   End If

' write 1024 'X' characters
' starting from beginning of file

   For i = 0 To 1023
    data(i) = Asc("X")
   Next i
   ixf.PutByte data, 1024, 0

' insert block of 256 bytes in the middle of the file
' on completion 'newsize' should be 1280

   newsize = ixf.Expand(512, 256)

' write 256 'O' characters into new block

   For i = 0 To 1023
    data(i) = Asc("O")
   Next i
   ixf.PutByte data, 256, 512

' finally remove inserted block
' on completion 'newsize' should be again 1024
' and file should contain only 'X' characters

   newsize = ixf.Shrink(512, 256)

    .
    .
    .

  End Sub

See Also

GetSize, SetSize, Expand, Shrink