If you simply begin writing data to a just-opened relative file, it will act much like a
sequential file, putting the data elements written by the first
PRINT#
statement in Record
#
I,
those written by the second
PRINT#
statement
in
record
#2
and so on. This means
each record must be written by a single
PRINT#
statement, using embedded carriage
returns within the data to separate fields that will be read
in
via one or more
INPUT#
statements later. However.
it
is
far better to explicitly specify which record number
is
desired via a
RECORD#
command to the disk. This al'ows you to access records
in
any
desired order, hopping anywhere
in
a
fiJe
with equal ease.
FORMAT FOR THE
RECORD#
COMMAND:
BASIC 7.0: RECORD #
file
#,
record number I ,offset]
BASIC 2.0:
PRINT#
15,
"P"
+ CHR$ (channel # + 96)
+CHR$
«record
#)
+ CHR$(
>record
#)
+ CHR$( offset)
where "file
#"
is
the file # specified
in
the current DOPEN statement for the specified
file, "record
number"
is the desired record number, "channel
#"
is
the channel number
specified in the current OPEN statement for the specified file,
"<record
#"
is
the low
byte
of
the desired record number, expressed
as
a two-byte integer,
">record
#"
is
the
high byte
of
the desired record number, and an optional
"offset"
value, if present,
is
the
byte within the record at which a following Read or Write should begin.
To fully understand this command, you must understand how most integers are
stored
in
computers based on the 6502 and related microprocessors.
In
the binary
arithmetic used by the microprocessor,
it
is
possible
to
express any unsigned integer from
0-255
in
a single byte.
It
is
also possible
to
store any unsigned integer from 0-65535
in
two bytes, with one byte holding the part
of
the number that
is
evenly divisible by 256,
and any remainder in the other byte. In machine language, such numbers are written
backwards, with the low-order byte (the remainder) first. followed by the high-order byte.
In
assembly language programs written with the Commodore Assembler, the low part
of
a
two-byte number
is
indicated by preceding its label with the less-than character
«).
Similarly, the high part
of
the numher
is
indicated
hy
greater-than
(».
SAFETY NOTE: GIVE EACH
RECORD#
COMMAND TWICE
To avoid the remote possibility
of
corrupting relative tile data,
it
is
necessary
to
give
RECORD#
command once before the Read or Write access and once after the
access.
EXAMPLES:
In
BASIC
7.0,
to
position the record pointer for
file
#2
to record number 3, type:
RECORD#2,3
56