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++
int main(int, char**)
{
IFile ixf;
long newsize;
BYTE data[1024];
ixf.SetLicenseKey("YOURLICENSEKEY");
ixf.Initialize();
ixf.Open("test.bin", TRUE);
if(ixf.GetSize() != 1024)
ixf.SetSize(1024);
memset(data, (int)'X', sizeof(data));
ixf.PutByte(data, 1024, 0);
newsize = ixf.Expand(512, 256);
memset(data, (int)'O', sizeof(data));
ixf.PutByte(data, 256, 512);
newsize = ixf.Shrink(512, 256);
.
.
.
return(0);
}
BASIC
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"
ixf.Initialize
ixf.Open "test.bin", TRUE
If ixf.GetSize() <> 1024 Then
ixf.SetSize 1024
End If
For i = 0 To 1023
data(i) = Asc("X")
Next i
ixf.PutByte data, 1024, 0
newsize = ixf.Expand(512, 256)
For i = 0 To 1023
data(i) = Asc("O")
Next i
ixf.PutByte data, 256, 512
newsize = ixf.Shrink(512, 256)
.
.
.
End Sub
See Also
GetSize,
SetSize,
Expand,
Shrink