Disk
File Input/Output
5~8
Remember that random disk files are stored as strings. Just as you must convert
fields
that
represent numeric values when you read them (using
eVI,
CVS, or CVD),
so must you convert numeric values to their corresponding strings when writing to a
random file, using the MKI$, MKS$, and MKD$ functions. Again, you must be sure
to allow for the number
of
bytes required
to
store the three numeric data types.
Integer
Single-precision value
Double-precision value
MKI$
MKS$
MKD$
2 bytes
4 bytes
8 bytes
For example, assume that you open a random file and define two fields: the first 2
characters as P$ and the next 4 characters as C$; the first field represents a part
number and the second its cost. The following statements open the file, set
P$ equal
to a variable
P A and
C$
equal to a variable CO, then write the two values to file
number
5:
10
OPEN
"R",
#5,
":F1
:PARTS"
20
FIELD
#5,
2 AS
P$,
4 AS
C$
30
LSET
P$
= MKI$ (PA)
40
RSET
C$
= MKS$ (CO)
50
PUT#5, 1
The part number,
of
course, must be between -32768 and 32767, and the cost
will
be
accurate to 7 digits.
Two other functions are related to random disk 110: LOC and LOF. LOF returns
the number
of
the last sector in the file; LOC returns the number
of
the sector that
was last read
or
written.
BASIC-SO