User`s guide
between maximum field length and average field length. A program that uses variable
record lengths must calculate the total length
of
each record as
it
is
entered,
to
be sure the
total
of
all fields doesn't exceed the space available.
WRITING
THE
RECORD
Here
is
an example
of
program lines to enter variable length fields for the above
file
design, build them into a single string, and send
thc;m
to
record number RE
in
file
number
3 (assumed
to
be a relative file that uses channel number 3).
150
CR$=CHR$(13)
2000
INPUT"NAME";NA$
2010
IF LEN(NA$)>30 THEN 2000
2020 INPUT"STREET";SA$
2030
IF LEN(SA$)>30 THEN 2020
2040 INPUT"CITY
& STATE" ;CS$
2050 IF LEN(CS$)>25 THEN 2040
2060 INPUT"ZIP CODE";ZP$
2070
IF LEN(ZP$)>
10
THEN 2060
2080
DA$=NA$+CR$+SA$+CR$+
CS$+
CR$+
ZP$
2090
IF LEN(DA$)<87 THEN 2120
2100
PRINT"RECORD TOO LONG"
2110 GOTO 2000
2120
RH=
INT(RE/256)
2130 RL=RE-256*RH
2140PRINT#15,
"P"
+ CHR$(96 + 3) +
CHR$(RL) + CHR$(RH)
2150
GOSUB 59990
2160
PRINT#3,DA$
2170
GOSUB 59990
2180 PRINT#15,"
P"
+CHR$(96 +
3)+
CHR$(RL) + CHR$(RH)
2190 GOSUB 59990
Carriage Return
Enter fields
And check length
of
each
Build output data string
Check its length
If
too long overall
Calculate record number
Position
to
record number
RE
Check for disk errors
Send data
to
it
Re-position for safety
To use the above program lines for the version with fixed length fields ,
we
would
alter a
few
lines as follows:
150
160BL$="
2000
INPUT"NAME"
;NA$
2005
LN
= LEN(NA$)
2010 IF LEN(NA$)>
27
THEN 2000
2015
NA$=
NA$+
LEFT$(BL$,
27
-
LN
)
2020 INPUT"STREET";SA$
2025
LN
= LEN(SA$)
62
Not needed this time
27
shifted space characters
Checking for different lengths
And padding
to
preset sizes
30
IF
LEN(SA$)>27 THEN 2020
~35
SA$=
SA$+
LEFT$(BL$,27-LN)
z()40
(NPUT"CITY & ST A
TE"
;CS$
2()45
LN
= LEN(CS$)
2()50
IF
LEN(CS$)>23 THEN 2040
2()55
CS$=
CS$+
LEFT$(BL$,23-LN)
zf11J
INPUT"ZIP CODE";ZP$
2()65
LN
= LEN(ZP$)
'df/O
IF
LEN(ZP$)>
10
THEN 2060
'df/5ZP$=ZP$+LEFT$(BL$,10-LN)
2()80
DA$
=NA$+
SA$+
CS$+
ZP$
2120
RH=
INT(REJ256)
2130
RL = RE-256*RH
2140
PRINT#l5,
"P"
+CHR$(96+
3) +
CHR$(RL) + CHR$(RH1 + CHR$
(I)
2150
GOSUB 59990
2160
PRINT# 3 ,DA$;
2170
GOSUB 59990
2180
PRINT#l5,"P"
+CHR$(96+
3) +
CHR$(RL) + CHR$(RH) + CHR$
(I)
2190
GOSUB 59990
Note lack
of
separators
Note added semicolon
If field contents vary
in
length, variable field lengths are often preferable. On the
Olhcr
hand, if the field lengths are stable, fixed
field
lengths are preferable. Fixed length
fields
are also required if you want
to
use the optional offset parameter
of
the
. Record#
command
to
point
at
a particular byte within a record. However, one warning must be
made
about using the offset this way . When any part
of
a record
is
written,
DOS
overwrites any remaining spaces
in
the record. Thus, if you must use the offset option,
never
update any field in a record other than the last one unless
all
succeeding fields will
also
be updated from memory later.
The above programs
are careful
to
match record lengths exactly
to
the space
available. Programs that don't do so
will discover that DOS pads short records out to full
size
with
fill
characters, and truncates overlong records
to
fill
only their allotted space.
When
a record
is
truncated, DOS will indicate error
51
, ''RECORD OVERFLOW'', but
short
records will be accepted without a DOS error message.
READING A
RELATIVE
RECORD
Once a relative record has been written properly
to
diskette, reading
it
back into
COmputer
memory
is
fairly simple, but the procedure again varies, depending on whether
it
USes
fixed or variable length fields . Here are the program lines needed
to
read back the
Variable
fields created above from record number RE
in
file and channel 3:
Jooo
RH=
INT(RE/256)
3010
RL=RE-256*RH
3020
PRINT#l5,"P"
+CHR$(96+3)+
CHR$(RL) + CHR$(RH) + CHR$ (
1)
63
Calculate record number
Position
to
record number RE