Classic Computer Magazine Archive COMPUTE! ISSUE 43 / DECEMBER 1983 / PAGE 236

Commodore Files For Beginners

Part 2

Jim Butterfield, Associate Editor

Expanding on his program examples from last month, Associate Editor Jim Butterfield suggests ways to improve and safeguard your files. For disk and tape users.

Creating A File By Program

We can repeat the file creation that we performed last month with direct statements, but this time we'll do it in a more typical way: as part of a program. Here come the statements we have seen before, with a few small enhancements:

100 PRINT "FILE CREATION"
110 INPUT "NAME OF FILE";N$

When the program runs, we must type in a file name. This might be the same name we used previously (STUDENTS). It's wise to choose a name that hasn't been used before. In fact, with disk it's mandatory: we cannot have two files with exactly the same name on one disk.

Now for the OPEN statement. For disk, we type:

120 OPEN 1, 8, 2, "0 : " + N$ + ", S, W"

For tape, we make line 120 read:

120 OPEN 1, 1, 2, N$

Now to write the data. Since we're writing a generalized program, it might be wise to ask the user to input the data. As soon as it is received, we'll write it to the file:

130 INPUT "NAME" ; A$
140 INPUT "STUDENT NUMBER" ; B$
150 INPUT "MARK" ; M
160 REM PRINT IT
170 PRINT#1, A$ ; CHR$(13);
180 PRINT#1, B$ ; CHR$(13);
190 PRINT#1, M ; CHR$(13);

We could make the program more friendly by asking ARE YOU SURE? in line 155, so that the user could reenter the information if a mistake had occurred.

Now that the record is written, we need to ask if there are any more:

200 PRINT
210 INPUT "MORE" ; X$
220 IF X$ = "Y" OR X$ = "YES" GOTO 130

When we get beyond this point, the user has signaled that the job is completed. All we need to do is CLOSE the file, and we're finished:

230 CLOSE 1
240 PRINT "FILE ";N$;" IS WRITTEN"

Trimmings For Disk

If we are using disk, we might add disk error checking. This tells us if we have problems—it's especially important at the time or opening the file. The extra lines for this would be added to the above program:

90 OPEN 15, 8, 15
95 PRINT#15,"10"
125 INPUT#15, E, E$, E1, E2
126 IF E THEN PRINT E$ : STOP

Lines 125 and 126 may be repeated after each disk activity, so we could see the same instructions at lines 205 and 206, and again at 235 and 236. You could put these two lines in a subroutine, but they are brief enough to repeat at the appropriate places. Finally, we should CLOSE the command channel with:

250 CLOSE 15

Always OPEN the command channel at the beginning of a program and CLOSE it at the end. Closing a command channel causes the disk to close any other channels it might have going; it could give you real trouble if performed too early.

Trimmings For Tape

You could remove the ;CHR$(13); ending from the PRINT#1 lines if you wish. But it might be best to leave it in place, so that your programs can be converted to disk operation without fuss.

If you have an original small-keyboard PET, you can't write to disk at all and may have trouble with cassette tape (blocks written too closely together). If you're serious about files, you might want to upgrade your machine.

A cassette tape file doesn't need to have a name, but use one anyway.

Reading It Back

It would be nice to bring the file back using direct statements, as we did the first time we wrote the information. However, we can't use INPUT# in direct mode, so we must write a program. Much of it will look familiar. First, we OPEN the file, then ask for the name:

100 PRINT "FILE READER"
110 INPUT "FILE  NAME" ; N$

For disk, we would write the OPEN statement as:

120 OPEN 1, 8, 2, N$

We don't need to specify the drive number as both will be checked. We don't need to specify ,S,R for sequential read because these options will be assumed. It doesn't hurt to specify everything, however.

For tape, we would OPEN with:

120 OPEN 1, 1, 0, N$

In fact, if there's only one data file on the tape, or if the one we want is the first, we could write OPEN 1 and everything else would be assumed.

130 INPUT*1, A$
140 INPUT#1, B$
150 INPUT#1, M

Now that we've input a record, let's print it out:

160 PRINT "NAME : {3 SPACES}" ; A$
170 PRINT "NUMBER : " ; B$
180 PRINT "MARK : {3 SPACES}" ; M

Are there any more records? The computer knows; and if we know how, we can ask the computer.

There's a variable in the computer called ST or STATUS. After every file operation — or more exactly, after every input/output operation — variable ST will be set as follows:

ST equals 0: file OK, more to come
ST equals 64: file OK, no more to come
ST other than 0 or 64: file has a problem

For our simple reading program, we can type:

190 IF ST = 0 GOTO 130

Thus, if the file is OK and is not at the end, we'll go back and get another record.

Finally, we CLOSE the file with:

200 CLOSE 1

RUN the above program, and the information we wrote to file STUDENTS will be recalled and printed out to the screen.

Try Your Hand At These

Our file program is a good working example. You might like to see if you can write some of the following variations:

If you have disk, add error checking. Then try creating errors (bad names) and see what happens.

Modify the program to print only student records for students named JONES.

Modify the program to count the number of students.

Modify the program to calculate an average grade.

We'll look at other aspects of sequential files next time around.