English
Language : 

NSB8 Datasheet, PDF (113/158 Pages) List of Unclassifed Manufacturers – BASIC interpreter for Z80 family
ITALIAN TECHNOLOGY
grifo®
RANDOM DATA ACCESS
BASIC keeps track of where it is supposed to read and write next in an open file by maintaining a
file pointer for it. This pointer specifies the number of bytes from the start of the file to the current
read/write position. This number is called a random file access. When a file is opened, its file pointer
is set to 0, meaning that the first data access will happen at the start of the file. You can change the
value of the pointer, and so access file fata beginning at any point in a file. This is called random
access and is one of the quickest means of storing and retrieving data in files because it is not
necessary to read all the data items in a file in order to get to the one you want. By changing the file
pointer to reference the location of the data item you seek, you can read or write it immediately.
A random address expression is added to a READ# or WRITE# statement in order to access data
randomly. The random address expression is a numeric expression following a percent sign (for
example: %R*5). The expression must evaluate to an integer from 0 to the file length value in bytes.
If an address expression is ever negative or greater than the limit given by the above formula, a
program error will occur.
In order to use random access, you must be able to determine the necessary random address of the
particular piece of data you want. The easiest way to do this is to require that all items in the file be
of the same type or size. For example, a file intended for random access might consist of all numbers,
or all 10 character strings. Alternately, a random access file might contain 100 records of 62 bytes
each. Each record might consist of 4 numbers in a row, plus a string of length 40.
How was the figure of 62 bytes for the record size computed? In order to find out how much disk
storage space a group of items will require, you must add up all the actual sizes of each of the
elements. Refer to section IMPLEMENTATION NOTES, for information on computing the storage
sizes for strings and numbers.
Knowing exactly how long each element or record is, you can treat the entire file as a huge array of
items or records, computing the random address of the Xth item in the file with the following
expression:
(X-1)*R
where R is the size of an individual record or item, given in bytes. Add a percent sign in front of this
expression, and you have a legal random address expression! To illustrate, given a file of strings, the
storage length of each being 42 bytes, then the first string would occur at address 0, which is
(1-1) * 42 = 0. The 50th string occurs at random address (50-1) * 42 = 49 * 42 = 2058.
Random access records may easily be updated in place, although you must still use NOENDMARK
to avoid the writing of an endmark after rewriting the record (the extra endmark could contaminate
the data in the next record!).
Here is a program which accesses any element of a random access file of 1000 strings, each of which
is 250 characters long:
10 REM Random string access.
20 OPEN #1,”RANDSTR”
30 DIM R$(250)
40 R=250+2
50 REM R is size of one item: see IMPLEMENTATION NOTES for details.
60 INPUT “WHICH STRING (1-1000, 0 TO QUIT)? “,I
70 IF I=0 THEN 130
80 IF I<1 OR I>1000 THEN 60
85 REM Check for out of range item number.
NSB8
Rel. 5.10
Page 101