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, and the selection is 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.setfilesize(fd.filesize/2) 'cut the file size in half

The fd.setfilesize 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 R/O 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 current pointer position invalid (that is, 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.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 3 data sectors will reduce its size by 512 bytes (amount of data in 2 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.

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