User`s guide

However, if you need to combine them, here is a safe way to do
it:
400 PRINT#5,NAME$;CHR$(13);STREET$;CHR$(13
);
CITY$
CHR$(13)
is
the carriage return character, and has the same effect as putting the p
rint
items
in
separate lines.
lf
you do this often, some space and time may be saved
by
previously defining a variable
as
equal to CHR$(13):
10
CR$=CHR$(13)
... 400
PRINT#5
,NAME$;CR$;STREET$;CR$;CITY$
The basic idea
is
that a proper sequential
dis)c
file
write, if redirected to the screen,
will display only one data item per line, with each succeeding item on the next line.
CLOSING A FILE WHEN
YOU
ARE DONE USING
IT
After you finish using a data
file
, it
is
extremely important that you Close
it.
Du
ring
the process
of
writing a file, data
is
accumulated
in
a memory buffer, and only written
out
lo
the physical cassette or diskette when the buffer fills .
Working this way, there
is
almost always a small amount
of
data
in
the buffer t
hat
has not been written to diskette or cassette yet, and which would simply be
Jost
if
the
computer system were turned off. Similarly, there are diskette housekeeping matters,
such
as updating the BAM (Block Availability Map)
of
sectors used
by
the current
file
, wh
ich
are not performed during the ordinary course
of
writing a
file
. This
is
the reason
for
having a Close statement. When we know we are done with a file, the Close statem
ent
will write the rest
of
the data buffer out to cassette or diskette, update the BAM,
and
complete the file's entry
in
the directory. Always Close a data
file
when you are d
one
using
it!
Failure to do so may cause loss
of
the entire
file!
However, do not
clo~e
the disk command channel until all other files have
been
Closed. The command channel (described
in
the last chapter), when used, should be
the
first
file
Opened, and the last
file
Closed
in
any program. Otherwise, remaining files
may
be closed automatically.
As
also described there, this may
be
used to advantage if a
program halts on an error while disk files are open.
FORMAT FOR THE CLOSE STATEMENT
CLOSE
file#
where
"file#
"
is
the same
file
numbe_r given
in
the desired
file
's current Open statem
ent
.
EXAMPLES:
To close the data
file
#5
used
as
an
example
on
the previous page, we would use
CLOSE 5
In
Commodore's CBM and PET computers, there
is
a Dclose statement, that, w
hen
used alone, closes
all
disk tiles
at
once. With a
bit
of
planning, the same can be done
in
Basic 2 and 3.5 via a program loop. Since there is
no
harm
in
closing a
file
that wa
sn
't
48
0
close every
file
you even think might be open before ending a program.
If
for
~~an:ple,
we always gave our files numbers between I and 10, we could close them all
with
9950 FOR
I=
I TO
10
9960 CLOSE I
9970
GOSUB 59990:REM CHECK FOR DISK ERRORS
9980 NEXT I
(assuming your program includes an error check subroutine like the one
in
Chapter 4)
READING FILE DATA: USING
INPUT#
Once information has been written properly to a diskette file,
it
may be read back into
the
computer with an
Input#
statement. Just
as
the
Print#
statement
is
much like the Print
statement, Input#
is
nearly identical to Input, except that the list
of
items following the
command word comes from a particular file instead
of
the keyboard. Both statements are
subject
toยท the same Jimitations--halting input after a comma .or colon, not
a~ceptin~
data
items
too large to
fit
in
Basic's Input buffer, and not acceptmg non-numenc data mto a
numeric variable.
FORMAT
FOR THE
INPUT#
STATEMENT
PRINT#file # ,variable list
where
"file#"
is the same
file
number given
in
the desired file's current Open statement,
and
"variable list"
is
one or more valid Basic variable names.
If
more than one data
element is to be input by a particular
Input#
statement, each variable name must be
separated from others by a comma.
EXAMPLES:
To
read back
in
the grades written with the
Print#
example, use:
300 FOR
CLASS=
I TO COURSES
310 INPUT#l,GRADE$(CLASS)
320 GOSUB 59990:REM
CHECK FOR DISK ERRORS
330
NEXT CLASS
(assuming your program includes an error check subroutine like the one on page 27).
To read back
in
the address data written by another Print# example,
it
is
safest to
USe:
800 INPUT#5,NAME$
810 GOSUB 59990:REM
CHECK FOR DISK ERRORS
820 INPUT#5,STREET$
830 GOSUB 59990:REM
CHECK FOR DISK ERRORS
840
INPUT#5,CITY$
850 GOSUB 59990:REM CHECK FOR DISK ERRORS
49