User`s guide

1410 OPEN 1,8,2,
"O:"+FI$+"
,L,"+CHR$(RL)
1420 GOSUB 59990
1430
RH=
INT(NR/256)
1440 RL = NR-256*RH
1450
PRINT#l5,
"P"
+CHR$(96+
2) +
CHR$(RL) + CHR$(RH)
1460 GOSUB 59990
1470
PRINT#l,CHR$(255);
1480 GOSUB 59990
1490
PRINT#l5
,
"P"
+
CHR$(96+
2) +
CHR$(RL) + CHR$(RH)
1500 GOSUB 59990
1510
CLOSE
1
1520 GOSUB 59990
9980
CLOSE
15
9990
END
59980 REM
CHECK
DISK SUBROUTINE
59990
INPUT#l5,EN,EM$,ET,ES
60000 IF
EN>
I
AND
EN<>50
THEN PRINT
EN
,EM$,ET
,ES:STOP
60010 RETURN
Begin to create desired file
Check for disk errors
Calculate length values
Position to last record
number
Send default character to it
Re-position for safety
Now the file can be safely
closed
And the command channel
closed
Before we end the
pro-
gram
Ignore
"RECORD
NOT
PRESENT''
Two
lines require additional explanatiop. When line 1470 executes, the disk drive will
operate for up to ten or more minutes, creating all the records in the file, up to the
maximum record
number
you selected
in
line 1390. This is normal, and only needs to be
done once. During the process you may hear the drive motor turning and an occasional
slight click as the head steps from track to track, everything
is
probably just fine. Second,
line
60000 above
is
different from the equivalent line
in
the error check subroutine given
earlier. Here disk error number
50 is specifically ignored, because it will be generated
when the error channel is
chec;ked
in
line 1460. We ignore it because not having a
requested record would only
be
ail error
if
that record had previously been created.
EXPANDING A RELATIVE FILE
What if you underestimate your needs and need to expand a relative file later? No
problem. Simply request the record number you need, even if
it
doesn't
currently exist
in
the file.
If
there is no such record yet, DOS' will create
it
as soon as you try to write
information
in
it, and also automatically create any other missing records below
it
in
number. The only penalty will be a slight time delay while the records are created.
60
WRITING RELATIVE FILE DAT A
The commands used to read and write relative file data are the same
Print#,
Input#,
and
Get#
commands used in the preceding chapter on Sequential files . Each command
is
used as described there. However, some aspects
of
relative file access do differ from
sequential file programming, and we will cover those differences here.
DESIGNING A KELA
TlVE
RECORD
As stated earlier
in
this chapter, each relative record has a fixed length, including all
special characters. Within that fixed length, there are two popular ways to organize
various individual fields
of
information.
One
is
free-format, with individual fields varying
·in
length from record to record, and each field separated from the next by a carriage return
character (each
of
which does take up I character space in the record). The other approach
is
to use fixed-length fields, that may
or
may not be separated by carriage returns.
If
fixed
length fields are not all separated by carriage returns, you will either need to be sure a
carriage return
is
included within each 88 character portion
of
the record. If this
is
not
done, you will have to use the
Get#
command to read the record, at a significant cost
in
speed.
Relative records
of
88 or fewer characters,
or
final portions
of
records that are 88 or
fewer characters in length, need not
end
in
a carriage return .
Jhe
1541
is
smart enough to
recognize the end
of
a relative record even without a finar carriage return. Though the
saving
of
a single character
isn't
much, when multiplied by the number
of
records on a
diskette, the savings could be significant.
Since each relative record must be written by a single
Print#
statement, the recom-
mended approach
is
to build a copy
of
the current record
in
memory before writing
it
to
disk.
It
can be collected into a single string variable with the help
of
Basic's many string-
handling functions, and then all written out at once from that variable.
Here
is
an example.
If
we are writing a 4-line mail label, consisting
of
4 fields named
"NAME'
',
"ST
REET"
,
"CITY
&
STATE",
and
"ZIP
CODE",
and have a total record
size
of
87 characters, we can organize it
in
either
of
two ways:
WITH FIXED LENGTH FIELDS
WITH VARIABLE LENGTH FIELDS
Field Name
Length
Field Name
Length
NAME
27 characters
NAME
31
characters
STREET
27 characters
STREET
31
characters
CITY & STATE
23
characters
CITY & STATE
26 characters
ZIP CODE
I 0 characters
ZIP
CODE
11
characters
Total length
87 characters
Potential length
99 character
Edited length
87
characters
With fixed length records, the field lengths add up to exactly the record length.
Since
the
total length
is
just within the Input buffer size limitation, no carriage return characters
are needed . With variable length records, we can take advantage
of
the vatiability
of
actual address lengths. While one name contains
27
letters, another may have only
15
,
and the same variability exists
in
Street and City lengths. Although variable length records
lose I character per field for carriage returns, they can take advantage
of
the difference
61