Experiment
#2
Reading a RAM File
Now that you have a data file stored in RAM, you can write a program
to
read and
display it. Clear memory using the NEW command and then enter the following
program from the keyboard:
500
CLS:OPEN"RAM:NAMES"
FOR
INPUT
AS
1
510
INPUT
#1,
N$
: PRINT
N$
520
IF
NOT
EOF(l)
GOTO
510
530
CLOSE
Execute this program.
You should see the display clear and then the list of names saved previously will
appear
as:
John
Smith
Peter
Wolf
Aloysius
T.
Cornpone
Jim
Shoe
Steele
Magnet
Ray
D.
O/Shack
Ok
II
Line
stt
The CLS statement clears the display. The OPEN statement defines the
RAM file
"NAMES.DO"
which will be used for input and
is
referenced with file
number
1.
Since no extension is given in the OPEN statement, the .DO extension is
assumed.
Line
518 The INPUT # 1 statement reads the next name
in
the data file and assigns it
to
the variable N$. Note that file number 1 refers to the RAM file
"NAMES.DO"
as
defined in the preceding OPEN statement.
The PRINT statement displays the name read from the data file.
Line
528 The IF statement tests for the end of the data file.
If
it is not the end of file
number 1, execution jumps back to line 510 to read another name.
If
the end of file
number 1 is reached, execution continues with line 530. Two new features of BASIC
are used in this statement.
First, the function
EOF(I),
returns a value of TRUE
if
the end of file number 1 has
been reached, or a value of
FALSE
if
the end has not been reached.
Next, the logical operator NOT is used to change the logical value of EOF(l).
IF EOF(l) is
TRUE, then NOT EOF(l) will
be
FALSE.
IF EOF(l) is FALSE, then NOT EOF(I) will be TRUE.
Line 538 The CLOSE statement terminates access to the file.
Since this program will be used later in this lesson, you should save it using the
command:
SAlJEI
II
EXP2"
125