User`s guide

EXAMPLE:
If
you are writing a grading program,
it
wou.ld
be convenient
to
simply tack on each
student's new grades to the end
o(
their existing grade files .
To
add data to the
"JOHN
PAUL
JONES"
file, we could type
OPEN 1,8,3, "O:JOHN PAUL
JONES.A"
In this case, DOS will allocate at least one more sector (block) to the file the first
time you append to it, even if you only add one character
of
information. You may also
notice that using the Collect or Validate command didn't correct the
file
size. On-the other
hand,
your data
is
quite safe, and if the wasted space becomes a problem, you can easily
-correct it by copying the file to the same diskette or a different one, and scratching
th
e
original file. Here's a sequence
of
commands that will copy such files to the original
diskette under the
:>riginal name, for ease
of
continued use:
BASIC 2:
PRINT#l5,"RO
:
TEMP=JOHN
PAUL
JONES"
PRINT#l5,"CO
:JOHN PAUL
JONES=TEMP"
PRINT#
15,'
'SO:
TEMP''
BASIC 3.5:
RENAME
"JOHN
PAUL
JONES"
TO
"TEMP"
COPY
"TEMP"
TO
"JOHN
PAUL
JONES"
SCRATCH
"TEMP"
If you are using Basic 2, be sure to open
file
15
to the command channel beforehand (i.e.,
with
OPEN 15,8,15) and close
it
afterwards (i.e., with CLOSE 15).
WRITING FILE DATA: USING PRINT#
After a sequential file has been opened to write (with a type and direction
of
",S,W"),
we use the
Print#
command to send data to it for storage on diskette.
If
you are
familiar with Basic's Print statement, you will find
Print#
works exactly the same way ,
except that the list
of
items following the command word
is
sent to a particular file ,
instead
of
automatically appearing on the screen. Even the formatting options (punctua-
tion and such) work in much the same way
as
in
Print statements. This means you have to
be sure the items sent make sense to the particular file and device used.
For instance, a comma between variables
in
a Print statement acts
as
a separator
in
screen displays, making each successive item appear
in
the next preset display field
(typically at the next column whose number
is
evenly divisible
by
10).
If
the same comma
is
included between variables going
lo
a disk file,
it
will again act
as
a separator, again
inserting extra spaces into the data. This time, however,
it
is
inappropriate, as the extra
spaces are simply wasted on the diskette, and may create more problems when reading the
file
bl!ck
into the computer. Therefore, you are urged
to
follow the following format
precisely when sending data to a disk file .
FORMAT FOR
THE
PRINT#
STATEMENT
PRINT#file
#,data
list
46
"°here
"file#"
is the same file number given
in
the desired file's current Open statement.
purin&
any given access
of
a particular file, the file number must remain constant because
.
1
serves
as
a shorthand way
of
relating all other file-handling commands back to the
~orrect
Open statement. Given a file number, the computer can look up everything else
a!JOUt
a file that matters.
The
"data
list"
is
the same
as
for a Print statement - a list
of
constants, variables
andfor expressions, including numbers, strings
or
both. However, it
is
strongly recom-
mended that each
Print#
statement to disk include only one data item.
If
you wish to
iflClude
more items, they must be separated by a carriage return character, not a comma.
Semicolons are permitted, but not recorded
in
the file, and do not result in any added
spaces
in
the file. Use them to separate items
in
the list that might otherwise be confused,
such
as
a string variable immediately following a numeric variable.
Note: Do not leave a space between
PRINT and
#,
and do not abbreviate the
command
as?#.
The correct abbreviation for
Print#
is
pR.
EXAMPLES:
To
record a few grades for John Paul Jones, using a sequential disk file
#1
previously
opened
for writing, we could use:
200 FOR
CLASS=
1
TO
COURSES
210:
PRINT#l,GRADE.$(CLASS)
220
NEXT CLASS
320 GOSUB 59990:REM
CHECK FOR DISK ERRORS
(assuming your program includes an error check subroutine like the one in the last
chapter).
In using
Print#
there
is
an
exception
to
the
requirement
to
check
for
disk
errors
after
every file-handling
statement.
When
using
Print#,
a single check
after
an entire set
of
data
has
been written
will
still detect
the
error,
so
long
as
the
check
is
made
before
any
other
file-handling
statement
or
disk
command
is
used.
You
may be familiar with
Print
statements in which several items follow each
other:
400 PRINT NAME$,STREET$,CITY$
To
get those same variables onto sequential disk file number 5 instead
of
the screen, the
best
approach would be to use three separate
Print#
statements,
as
follows:
400
PRINT#5,NAME$
410
PRINT#5
,STREET$
420 PRINT#5,CITY$
47