, 1
A
good
way
to CLOSE a file that
is
being read
is
to test for
an
End
of File
(EOF)
with
the STATUS WORD (ST). When the data file was written.
an
EOF
mark was
written
at the end of the file.
When
an
EOF
mark
has
been found. the file
status equals 64 and the file may
be
closed. You may test for
an
EOF
mark and
close the file in one statement:
IF
ST=64
TH
EN
CLOSE
1
When
ST
equals 64. the file
will
automatically
CLOSE.
Reading Numbers
with
INPUT#
Previously
we
wrote the pragram NUM.PRINT# to
write
the numbers 1
through 10
on
the data tape NUMBERS. where each number was
written
as
a
single record.
Now
we
will
write
a program called NUM.INPUT# to read the ten
numbers from the NUMBERS data file. and print them on the screen.
To read numbers and strings from a data tape the INPUT# command
is
used. INPUT# reads one data item at a time. the data items being separated by a
semicolon. comma. or carriage return.
INPUT#f.var
where:
is
the logical file number (1-255. matching the file number in the
OPEN
and
CLOSE
command).
var
is
the variable
name(s)
of the data to
be
read.
The first few lines of NUM.INPUT# instruct the user to load the data tape.
These lines are identical to the first three lines of NUM.PRINT#.
At
line 30
is
the
wait
loop allowing the user time to mou
nt
the data tape.
After
mounting the tape.
key
RETURN;
the program continues at the next line.
10
PR!tH"~.
READ
NUMER
1C
DATA
TAPE
••
":
PR1
NT
20
PRINT"
••
MOUNT
TAPE;
PRESS <RETURN.>
WHEN
READY
••
":PRINT
30
GET
A$:
IF
A$=""
THEN
30
Before we can read any data.
we
must open the data file. Line 40 opens file
#1.
physical device
#1.
and secondary address 0
(OPEN
for
read)
with
filename NUM-
BERS.
4(1
PF~INT"
••
OpnlING
DATA
FILE
••
":
OPEN 1
..
1,
(1
.•
"NUMBERS" :
F'RItH
Next. a
FOR
...
NEXT
loop
is
set up to
read
the first ten data items fram the
tape and print to the screen:
50
FOR
1=1
TO
10
60
INPUT#l,
N
read
N from tape
7(1
PR
1
NT
N print N
on
screen
80
NEXT 1
257