Chapter
71
Disk
Files
After typing this program, save it and run it. Then, enter the
following data:
2-DIGIT CODE, 0 TO END? 20
IENTER]
2-DIGIT CODE,
0
TO END? 0
[ENIER)
BASIC stores
SMITH,
34.55, and 567-9000 in Record 20 of
file
1isting.bas.
Accessing
a
Direct Access File
1.
Open the file in Random mode:
OPEN
"R",
1
,"listing.dat",32
2.
Use the FIELD statement to allocate space in the buffer for
the variables that will be read from the file. For example:
FIELD
1,
20
AS
N$,
4
AS
A$,
8
AS
P$
3.
Before you use the GET statement to read the record, you can
check to see if the record is in your file. Set a variable in
your program equal
to
the record size you used in the
OPEN
statement. LOF returns the length of the file in bytes. The to-
tal number of bytes in the file divided by the record size is
equal to the largest record number in the file. An attempt
to
access
a
record number greater than the largest record num-
ber in the file results
in
an "Input past end" error.
For
example:
RECSIZE
=
32
IF
CODE%
>
(LOF(1)
/
RECSIZE%) THEN 1000
4.
Use the GET statement to read the desired record from a di-
rect disk file into a buffer. For example:
GET
1,
CODE%
gets the record numbered CODE% and reads it into Buffer
1.
73