Classic Computer Magazine Archive COMPUTE! ISSUE 25 / JUNE 1982 / PAGE 58

The Beginner's Page

Files Versus Programs

Richard Mansfield
Assistant Editor

A friend of mine bought a computer a couple of months ago. I asked him what he found to be the most baffling, the toughest thing to learn to do with it. "Files," he said without hesitation, "I can't make the dang things work."

Files were also the most confusing thing to me when I first bought a computer. In fact, several years ago, handling files mystified nearly everyone who hadn't had previous experience with computers. Some of the early newsletters for home computerists were full of discussions and techniques on how to make files work.

They are somewhat more tricky than most programming techniques – there is more responsibility left up to the programmer. OPEN, CLOSE, PRINT # and INPUT # are so useful, however, that they deserve to be studied a little until they are understood.

Because file handling (also called data base management) requires a bit of explanation, we can look this month at the general differences between programs and files. In the July issue, we'll get down to specific filing techniques.

Telling Them Apart

The first step is to realize that tapes or disks store two different things – programs or files. (Some books refer to programs saved on tape or disk as "program files," but that terminology is worse than redundant, it's also confusing.) A BASIC program is a list of "lines" and each line contains instructions to the computer. These instructions are to be carried out during a RUN of the program. That is, the instructions are followed in order, from the lowest line number to the highest, when you type the word RUN. A data file, by contrast, is raw information like a page in a telephone book, without any instructions about what to do with that information.

When programs are SAVEd onto a disk or tape they can later be LOADed back into the computer to be RUN at any time in the future. Any programs you write into the computer will stay there only as long as the computer is turned on. So, to build a library of programs, you must SAVE them on tape or disk. They are SAVEd as if the tape or disk (let's just call them "magnetic memory") were given a photo of the program that was in the computer at the time of the SAVE. BASIC keeps track of how large a program is, where it starts and ends in the computer's memory cells, so it knows just what to "photograph" when you ask for a SAVE.

You, however, are far more responsible for handling the storage of files. BASIC doesn't supervise their storage or recall nearly as completely as it does with programs. You must do several things to create a file on magnetic memory and several things to get it back into the computer later. You establish the size of the file, the divisions between items in the file (called delimiters), and the order of the items. We'll illustrate this next month, but first let's see, visually, how programs and files differ:

A typical recipe will have both a "file" and a "program" in it:

MEATLOAF

1 lb. Hamburger

1 cup bread crumbs

1/2 cup milk spices

1. Mix ingredients.

2. Form into loaf.

3. Bake 45 minutes at 325.

Steps one through three are clearly a "program" of sorts. The first clue is that each item starts with a number, indicating the order in which the steps are performed. The ingredients – standing by themselves as raw data – are a file. Just as the ingredients "file" in the example above is acted upon by the cooking instructions "program," a computer program acts upon a data file.

On Magnetic Memory

Here's a simple program which will create a tape file on a Commodore computer:

10 DATA AAA, BBB, CCC
20 OPEN 1, 1, 1, "FILE"
30 FOR I = 1 TO 3
40 READ D$
50 PRINT #1, D$
60 NEXT I
70 CLOSE 1

PRINT# (usually pronounced "print-number") is an entirely different command from PRINT and the punctuation, as usual in programming, must be exact. Line 40 is interesting because we keep READing D$ over and over to use it as a temporary holding place until we can PRINT# it to a magnetic memory. D$ isn't anything in itself (it varies, it's a variable). READ will pick out each datum from the DATA line in turn, keeping track of the last one that was READ.

In any case, after this program is RUN, the magnetic memory would contain a file. If we could look at that file on the tape the way we would look at a photograph, we would see a row of numbers. The number 65 stands for the letter A and 13 represents a carriage return. Here's what the photograph would look like:

Figure 1: A File

A program (with its line numbers and instructions) would be longer than a file containing the same data:

10 PRINT "AAA"
20 PRINT "BBB"
30 PRINT "CCC"

The Microsoft BASIC version of this short program would look like this in the computer's memory or on magnetic memory after a SAVE:

Figure 2: The Program

We still find the AAA's, BBB's, and CCC's in there, but surrounded by line numbers, the 34's (quotes), and zeros which show where one thing ends and another begins (delimiters).

Notice those three zeros at the end. They show the computer where the entire program ends. There is nothing in Figure 1 to show where the file ends (when reading a file into the computer, you've got to stop INPUT#ing at the right time or you'll start bringing in nonsense). Next issue we'll explore just how to signal the end of a file and how files are created, updated, and, when necessary, erased.