445
TIDE and Tibbo BASIC User Manual
©2000-2008 Tibbo Technology Inc.
is fd.f1! When the pointer is at maximum, it effectively points at the file
position that doesn't exist yet. So, writing to the file at this position will append
new data to the end of the file. Writing to the file when the pointer is not at
maximum will overwrite existing data.
, the pointer is set to 1 if the file has any data in it (fd.
filesize<>0), or 0 if the file is empty (fd.filesize=0). It is not possible to set the
pointer to zero if the file is not empty. The pointer will be set to zero automatically
if the file becomes empty.
In the following example, we append the data to the end of the file, then read the
data from the beginning of the file:
Dim
s
As
String
'open a file 'on' file number 3
fd.filenum=3
fd.open("SomeFile")
fd.setpointer(fd.f1)
'works always -- no matter whether the file
is empty or not
fd.setdata("Append this to the file")
fd.setpointer(1)
'go back to the beginning of the file
s=fd.getdata(10)
'read 10 bytes from the beginning of the file
As you append new data to the file, the file will grow larger. New data sectors will
be allocated and added to the file automatically as needed. You will get the 7-
PL_FD_STATUS_DATA_FULL error if the disk runs out of free sectors.
The fd. object uses a RAM buffer as an intermediary storage for the sector data.
When you access a certain data sector, the contents of this sector are loaded into
the RAM buffer automatically. When you change the data in the sector, it is the
data in the RAM buffer that gets changed. The changed contents of the RAM buffer
will not be saved back to the flash memory until the contents of another sector
must be loaded into the buffer. So, for example, if you do this...
fd.setdata("Write this to a file")
... and then leave the disk alone, then the new data may stay in the RAM buffer
indefinitely. It may get lost -- for example, if you reboot the device. To prevent
this,
method -- either one will cause the
changed data in the RAM buffer to be saved back to the flash memory. Note that
the fd.flush method does not depend on the current fd.filenum value and works
globally on any most recently changed file.
Finally, do take note of the
R/O property. This property reflects the
number of the data sectors corresponding to the current position of the pointer.
This information can be useful, for example, to
the data sector before
making changes to the file.
Removing Data From Files
Using the
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
property.
method cuts the end portion of your file and preserves a
443
448
466
478
454
480
463
481