Removing Data From Files

Discussed in this topic: fd.setfilesize, fd.cutfromtop.


Using the fd.setdata method can only increase the size of your file. To reduce the file size (i.e., remove some data from it), use one of the two following methods. Both methods work on a currently selected file, with the selection made through the fd.filenum property.

The fd.setfilesize method cuts the end portion of your file and preserves a specified amount of bytes in the beginning of the file:

Tibbo BASIC
'open a file 'on' file number 4
fd.filenum=4
fd.open("SomeFile")
fd.setfilesize(fd.filesize/2) 'cut the file size in half

The fd.setfilesize method can't be used to enlarge your file, only to make it smaller. Data sectors previously allocated to the file will be "released" (marked unused) if they become unnecessary due to the reduction in the file size. The first data sector of the file, however, will always remain allocated, even when the file size is set to 0.

The size of the file, indicated by the fd.filesize read-only property, will be corrected downwards to reflect the amount of data left in the file.

The pointer position will be affected by this method. If the file becomes empty, the pointer will be set to 0. If the new file size is not zero, but the new size makes the current pointer position invalid (i.e., fd.pointer > fd.filesize + 1) then the pointer will be set to fd.filesize + 1.


The second method — fd.cutfromtop — removes a specified number of sectors (not bytes) from the beginning of the file and leaves the end portion of the file intact:

Tibbo BASIC
'open a file 'on' file number 2
fd.filenum=2
fd.open("SomeFile")
fd.cutfromtop(3) 'remove three front sectors from the file (that is, remove up to 3*256= 768 bytes of data)

The size of the file will be corrected downwards in accordance with the amount of removed data. For example, performing fd.cutfromtop(2) on a file occupying three data sectors will reduce its size by 512 bytes (the amount of data in two sectors removed). Performing fd.cutfromtop(3) will set the file size to 0.

The pointer position is always reset as a result of this method execution. If the new file size is 0, then the pointer will be set to 0 as well. If the file size is not 0, then the pointer will be set to 1.

The fd.setfilesize and fd.cutfromtop methods make changes to the sectors of the flash disk. For the highest possible reliability, use disk transactions when invoking these methods.