Reading File Data Using Input - Commodore 1541 User Manual

Hide thumbs Also See for 1541:
Table of Contents

Advertisement

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
Print
items in separate lines
.
If you do this often
,
some space and
time
may be
saved
by
previously defining a variable as equal to CHR$(
13):
LO CR$=CHR$(13)
...
400 PRINT#5 ,NAME$;CR$;STREET$;CR$;CITY$
The basic idea is that a proper sequential disk 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.
During
the process of writing a file
,
data is accumulated in a memory
buffer,
and only
written
out
to the physical cassette or diskette when the buffer fills
.
Working this way, there is almost always a small amount of data in the
buffer thai
has not been written to diskette or cassette yet
,
and which would simply be lost
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
, which
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
statemem
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
done
using it! Failure to do so may cause loss of the entire file!
However,
do not close 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
,
reruaining
files
ma)
be closed automatically
.
As also described there, this may be used to
advantage
if
1
program halts on an error while disk files are open
.
FORMAT FOR THE CLOSE STATEMENT
CLOSE file#
where
"
file#
"
is the same file number given in the desired file's current Open
statement
EXAMPLES
:
To
clo~e
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,
wn
e
r
used alone, closes all disk files at once
.
With a bit of planning, the same can be
done
~
Basic
2 and 3.5 via
a
program loop
.
Since there is no harm in closing a file that
wasn
48
apen. close every file you even think might be open before ending a program. If for
eitarnple, we always gave our files numbers between 1 and
10,
we could close them all
~th
9950 FOR I
=
1TO10
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 limitations-halting input after a comma or colon
,
not accepting data
items too large to fit in Basic
'
s Input
buffer,
and not accepting non-numeric data into 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 lNPUT#5 ,STREET$
830 GOSUB 59990:REM CHECK FOR DISK ERRORS
840
INPUT#5,CITY$
850 GOSUB 59990:REM CHECK FOR DISK ERRORS
49

Advertisement

Table of Contents
loading

Table of Contents