
desired person out
of
a thousand
in
under
15
seconds, a feat no sequential
file
progra
rn
could match.
FILE
LIMITS
. One
of
the nicest aspects
of
relative files
is
that all this
is
done for you without your
havmg to worry at all about exactly where on the diskette's surface a given record will
be
stored, or whether
it
will
fit
properly within the ;current disk sector,
or
need to
be
extended
onto. the next available sector.
DOS takes care
of
all that for you . All you need
to
do
is
specify how long each record is,
in
bytes, and how many records you will need. DOS
wi
ll
do the rest, and organize things
in
such a way that
it
can quickly
find
any record
in
the
fil
e
as soon
as
it
is
given its record number (ordinal position within the file). '
The only limit that will concern you,
is
that each record must
be
the same size,
an
d
the record length you choose must be between 2 and 254 characters. Naturally the entire
file
also has to
fit
on your diskette too, which means that the more records you need,
th
e
shorter each must be.
CREATING
A
RELATIVE
FILE
When a relative file
is
to be used for the first time, its Open statement will create
th
e
file;
a~e~
that, the same Open statement will
be
used to re-open the
file
for both reading
and wntmg.
FORMAT STATEMENT
TO
OPEN A RELATIVE FILE:
OPEN file
#,
device
#,
channel
#,
"drive
# :
file
name,
L,"
+ CHR$ (record
length)
where
"file#"
is
the file number, normally
an
integer between I and 127·
"device#"
is
the device
numbe~
to
be
used, normally 8 on the 1541;
"channel#"
sel~cts
a particular
channel along which communications for this
file
can take place, normally between 2
an
d
14;
"drive
#"
is
the drive number, always 0 on the 1541; and "file
name"
is
the
fil
e
?ame, with a maximum length
of
16
characters. Pattern matching characters are allowed
'.~
the name
~~~n
acc~ssing
an
existing file, but not when creating a new one. The
record length.
1s
the size
of
each record within the
file
in
bytes used, including carria
ge
returns, quotation marks and other special characters.
56
Notes:
I.
Do not precede the drive number with the
"at"
sign(@)
; there
is
no
reason
to replace a relative file.
2. ,L ,
"+
CHR$(record length)
is
only required when a relative
file
is
first
created, though
it
may used later, so long as the "record length"
is
the same
as
when the file was first created. Since relative files may be read from or written to
alternately and
with equal ease, there
is
no need to specify Read
or
Write mode
when opening a relative file.
3.
"file#",
"device#"
and
"channel#"
must be valid numeric constants,
variables
or
expressions. The rest
of
the command must be a valid string literal,
variable
or
expression.
4.
Only 1 relative file can be open at a time on the 1541, although a sequential
file
and the command channel may also be open
at
the same time.
EXAMPLES:
To create
or
re-open a relative file named
"GRADES",
of
record length 100, use
OPEN
2,8,2,
''GRADES,L,''
+CHR$(100)
To re-open an unknown relative file
of
the user's choice that has already been
created, we could use
200
INPUT''WHICH FILE'';FJ$
210 OPEN 5,8,5,FIS
USING
RELATIVE
FILES:
RECORD#
When a relative file
is
opened for the first time,
it
is
not quite ready for use . Both to
save time when using the
file
later, and to assure that the file will work reliably,
it
is
necessary to create several records before closing the file for the first time.
At
a minimum,
enough records to
fill
more than 2 disk sectors (512 bytes) should be written.
In
practice,
most programs go ahead and create as many records
as
the program
is
eventually expected
to
use.
That approach has the additional benefit
of
avoiding such problems
as
running out
of room on the diskette before the entire file
is
completed.
lf
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
#1,
those written by the second
Print#
statement
in
record
#2
and so on . (As this
implies, each relative record must be written by a single
Print#
statement, using embed-
ded
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 allows you
to
access records
in
any desired order, hopping anywhere
in
a file with equal ease. Properly used,
it
also
avoids a subtle error (bug) common to all Commodore disk drives.
57
Comentarios a estos manuales