
DEMONSTRATION
OF
SEQUENTIAL FILES
Use the following program for your first experiments with sequential files .
Comments have been added to help you better understand it.
150
CR$=CHR$
_(13)
1600PEN
15,8,15
170
PRINT CHR$(147):REM CLEAR
SCREEN
190
PRINT"**
WRITE A
FILE**"
210 PRINT
220 OPEN
2,8,2,"@0:SEQ
FILE,S,W"
230 GOSUB 500
240 PRINT"ENTER A WORD,
THEN A
NUMBER''
250
PRINT"OR
'END,O' TO
STOP"
260 PRINT
270 INPUT A$,B
280
PRINT#2,A$;CR$;B
290 GOSUB 500
300
IF
A$<>"END"
THEN 270
310
PRINT
320 CLOSE 2
340
PRINT"**
READ SAME FILE
BACK**"
360 PRINT
370 OPEN
2,8,2,
"O:SEQ
FILE,S,R"
380 GOSUB 500
390
INPUT#2,A$,B
400
RS=ST
410 GOSUB 500
420 PRINT
A$,B
430
IF
RS
= 0 THEN 390
440 IF
RS<>64
THEN
PRINT''STATUS=
'';RS
450CLOSE
2
460 END
480 REM
**
ERROR CHECK S/R
**
500
INPUT#l5,EN,EM$
,ET,ES
510 IF EN>O THEN PRINT
EN ,EMS.ET ,ES:STOP
520 RETURN
54
Make a carriage return variable
Open demo
file
with replace
Check for disk errors
Accept a string
& number
from keyboard
Write them
to
the disk file
Until finished
Tidy up
Reopen same
file
for reading
Read next string
& number from
fil
e
Remember
file
status
Display
file
contents
until done,
unless there's an error
Then quit
A Basic 3.5-only version could
replace line
500 with
500
IF
DS>O THEN PRINT
DS$:STOP
and delete line 510
CHAPTER6
RELATIVE
DATA
FILES
TffE
VALUE
OF
RELATIVE ACCESS
Sequential files are very useful when you 're just working with a continuous stream
of
data-
i.e., information that can be read or written all at once. However, sequential files
are
not useful or desirable
in
some situations. For example, after writing a large list
of
mail
labels, you wouldn't want
to
have to re-read the entire list each time you need a
person's record. Instead, you need some kind
of
random access, a way to get to a
particular label
in
your
file
without having to read through all those preceding
it
first.
As
an
example, compare a record turntable with a cassette recorder. You have to
listen to a cassette from beginning to end, but a turntable needle can be picked up
at
any
time, and instantly moved to any spot on the record.
Your disk drive works like a turntable
in
that respect.
In
this chapter we will learn about a type
of
file
that reflects this flexibility.
Actually, two different types
of
random access files may be used on Commodore
disk drives: relative files and random files. Relative files are much more convenient for
most
data handling operations, but true random access
file
commands are also available to
advanced users, and will be discussed
in
the next chapter.
FILES, RECORDS,
AND
FIELDS
When learning about sequential files, we did not worry about the organization
of
data
within a file, so long
as
the variables used to write the
file
matched up properly with those
which read
it
back into the computer. But in order for relative access
to
work, we need a
more
structured and predictable environment for our data.
The structure we will use
is
similar to that used
in
the traditional filing cabinet.
In
a
traditional office, all customer records might be kept
in
a single
file
cabinet. Within this
file,
each customer has a personal record
in
a
file
folder with their name on it, that
contains everything the office knows about that person. Likewise, within each
file
folder,
there may be many small slips
of
paper, each containing one bit
of
information about that
customer, such as a home phone number,·or the date
of
the most recent purchase.
In
a computerized office, the
file
cabinet
is
gone, but the concept
of
a
file
containing
all
the information about a group or topic remains. The
file
folders are gone too, but the
notion
of
subdividing the
file
into individual records remains. The slips
of
paper within
the
personal records are gone too, replaced
by
subdivisions within the records, called
fields
. Each field
is
large enough to hold one piece
of
information about one record
in
the
file.
Thus, within each
file
there are many records, and within each record there are
typically many fields.
A relative
file
takes care
of
organizing the records for you, numbering them from I to
whatever,
by
ones, but the fields are up to you
to
organize. Each record will
be
of
the
same size, but the
1541
won't insist that they all
be
divided the same way . On the other
hand, they normally will all be subdivided the same way, and if
it
can be known
in
advance exactly where each field starts within each record, there are even fast ways
to
access a desired field within a record without reading through the other fields . As
all
of
this implies, access speed
is
a primary reason for putting information into a relative disk
file.
Some well-written relative
file
programs are able to
find
and read the record
of
one
55
Comentarios a estos manuales