User`s guide

CHAPTERS
SEQUENTIAL DATA FILES
THE
CONCEPT
OF
FILES
A file on a diskette
is
just like a file cabinet
in
your
office-an
organized place to p
ut
things. Nearly everything you put on a diskette goes
in
one kind
of
file
or
another. So
far
all
we've
used are program files, but there are others,
as
we
have mentioned.
In
th
is
chapter we will learn about sequential data files .
As we just suggested, the primary purpose
of
a data file
is
to
store the contents of
program variables, so they won't be lost when the program ends. A sequential data file
is
one
in
which the contents
of
the variables are stored
"in
sequence,'' one right after
another, just
as
each link
in
a chain follows the previous link. You may already be famili
ar
with sequential files from using a DATASSETIE™, because sequential files on diske
tte
are just like the data files used on cassettes. Whether on cassette or diskette, sequential
files must be read from beginning
to
end, without skipping around
in
the middle.
When sequential files are created, information (data)
is
transferred byte by byte,
through a buffer, onto the magnetic media. Once
in
the disk drive, program files,
sequential data files, and user files all work sequentially. Even the directory acts like a
sequential file .
To
use sequential files properly, we will learn some more Basic words
in
the ne
xt
few pages. Then we'll put them together
in
a simple but useful program.
Note: Besides sequential data files, two other file types are recorded sequentially
on a diskette, and may be considered varying forms
of
sequential files . They are
program files, and user files . When you save a program on a diskette,
it
is
saved
in
order from beginning to end, just like the information
in
a sequential data file. The
main difference
is
in
the commands
y~u
use to access it. User files are even more
similar to sequential data files--differing, for most purposes,
in
name only. User
files are almost never used, but like program files, they could be treated
as
though
they were sequential data files and are accessed with the same commands.
For the advanced user, the similarity
of
the various
file
types offers the
possibility
of
such advanced tricks
as
reading a program file into the computer a
byte (character)
at
a time and rewriting
it
to
the diskette
in
a modified form . The
idea
of
using one program to write another
is
powerful, and available on the
Commodore disk drives.
OPENING A FILE
One
of
the most powerful tools
in
Commodore Bllsic
is
the Open statement. With
it
,
you may send almost any data
almo~t
anywhere, much like a telephone switchboard that
can connect any caller to any .destination.
As
you might expect, a command that can do
this much
is
fairly complex. You have already used Open statements regularly
in
some of
your diskette housekeeping commands.
42
Before we study the format
of
the Open statement, let's review some
of
the possible
devices
in
a Commodore computer system:
pevlce
#:
Name:
0 Keyboard
I
DATASSETTE
2 RS232
3 Screen
4 Printer
8 Disk drive
Used for:
Receiving input
from
the computer operator
Sending and receiving information
from
cassette
Sending and receiving information
from
a Modem
Sending output
to
a video display
Sending output
to
a hard copy printer
Sending and receiving information from diskette
Because
of
the flexibility
of
the Open statement,
it
is
possible for a single program
statement to contact any one
of
these devices, or even others, depending on the value
of
a
single character
in
the command. Often an Open statement
is
the only difference between
a program that uses a
DATASSETIE™ and one using the 1541.
If
the character
is
kept
in
a variable, the device used can even change each time that part
of
the program
is
used ,
sending data alternately and with equal ease to diskette, cassette, printer and screen.
REMEMBER
TO
CHECK FOR DISK ERRORS!
In the last chapter we learned how
to
check for disk errors after disk com-
mands
in
a program. It
is
equally important to check for disk errors after using file-
handling statements. Failure to detect a disk error before using another file-
handling statement could cause loss
of
data, and failure
of
the Basic program.
The easiest way to check the disk
is
to follow all file-handling statements with
a Gosub statement to an error check subroutine.
EXAMPLE:
840 OPEN 4,8,
4,"0:DEGREE
DAY
DATA,S,W"
850 GOSUB 59990:REM CHECK FOR DISK ERRORS
FORMAT FOR THE DISK OPEN STATEMENT:
OPEN
file#
,
device#,
channel
#,"drive
#:file name,file type.direction"
where:
"file
#"
is
an
integer (whole number) between I and· 255.
If
the
file
number
is
greater than 127, a line-feed character
is
inserted after each carriage return
in
the
fil.e
opened. Though this may
be
helpful
in
printer files,
it
will cause severe problems
in
disk
files, and
is
to be avoided at all costs. Do not open a disk file with a file number greater
than
127. After the file
is
open, all other file commands will refer
to
it
by
the number
given here. Only one file can use any given file number
at
a time.
"device
#"
is
the number, or primary address,
of
the device to be used. This
number
is
an integer
in
the range 0-31, and
is
normally 8 on the 1541.
43