14.6.10 Accessing Records by Record File Address
A Record File Address (RFA) uniquely specifies a record in a
file. Accessing records by RFA is therefore more efficient and
faster than other forms of random record access
Because an RFA requires six bytes of storage, BASIC has
a special data type, RFA, that denotes variables that con-
tain RFA information. Variables of data type RFA can be
used only with the I/O statements and functions that use
RFA information, and in comparison and assignment state-
ments. You cannot print these variables or use them in any
arithmetic operation. However, you can compare RFA vari-
ables using the equal to ( = ) and not equal to (<>) relational
operators.
You cannot create named constants of the RFA data type.
However, you can assign values from one RFA variable to
another, and you can use RFA variables as parameters.
Accessing a record by RFA requires the following steps:
The GETRFA function returns the RFA of the last record
accessed on a channel. Therefore, you must access a record
in the file with a GET, FIND, or PUT statement before using
the GETRFA function. Otherwise, GETRFA returns a zero,
which is an invalid RFA.
The following example declares an array of type RFA con-
taining 100 elements. After each PUT operation, the RFA of
the record is assigned to an element of the array. Once the
RFA information is assigned to a program variable or ar-
ray element, you can use the RFA clause on a GET or FIND
statement to retrieve the record.
DECLARE RFA R_array(1 TO 100)
DECLARE LONG I
MAP (XYZ) STRING A = 80
OPEN "TEST.DAT" FOR OUTPUT AS FILE #1, &
SEQUENTIAL, MAP XYZ
FOR I = 1% TO 100%
.
.
.
PUT #1
R_array(I) = GETRFA(1%)
NEXT I
You can use the RFA clause on GET or FIND statements for
any file organization; the only restriction is that the file must
reside on a disk that is accessible to the node that is executing
the program. An RFA value is only valid for the life of a spe-
cific version of a file. If a new version of a file is created, the
RFA values may change. If you attempt to access a record
with an invalid RFA value, BASIC signals a run-time error.
The following example continues the previous one. It ran-
domly retrieves the records in a sequential file by using RFAs
stored in the array.
DECLARE RFA R_array(1% TO 100%)
DECLARE LONG I
MAP (XYZ) STRING A = 80
OPEN "TEST.DAT" FOR OUTPUT AS FILE #1, &
SEQUENTIAL, MAP XYZ
FOR I = 1% TO 100%
.
.
.
PUT #1
R_array(I) = GETRFA(1%)
NEXT I
WHILE -1%
PRINT "Which record would you like to see";
INPUT "(type a carriage return to exit)";Rec_num%
EXIT PROGRAM IF Rec_num% = 0%
GET #1, RFA R_array(Rec_num%)
PRINT A
NEXT