Contents / Part 3 / Previous chapter / Next chapter / Index


31 File handling

You are probably already aware that as well as storing computer programs on cassette or disc, you can store "data". By "data" we mean sets of numbers or words. We might, for example, store a set of names and telephone numbers. This set of data is called a file. A set of BASIC statements are provided to enable you to read information from files, to write information to a new file, to update an existing file, to delete a file, to find out how big a file is, to move to a certain record in a file, to check if you have reached the end of a file and to obtain a catalogue of all the files that exist. There are also several other statements for performing other actions on the files.

One of the major features of the BBC Computer system is that exactly the same statements are used no matter which 'filing system' is in use. A number of different filing systems are available including cassette, disc and network systems. Programs written to work an a cassette file system will usually work unmodified on a disc system. See page 400 for details of other file systems.

Firstly here is a summary of all the file handling statements available in BBC BASIC.

*CAT gives a catalogue of all data files and programs on the cassette or floppy disc. It takes a very long time on a cassette.

OPENIN Opens a file so that it can be read.

OPENOUT Opens a new (empty) file for writing.

INPUT# Reads data from a file into the computer.

PRINT# Writes data from the computer into a file.

BGET# Reads a single character (byte) from a file.

BPUT# Writes a single character (byte) to a file.

PTR# Can be used either to find out which record we are

about to read (or write) or to move to a specific record. (Not cassette)

EXT# Indicates how long a file is. (Not cassette)

EOF# Indicates whether or not the end of a file has been reached

CLOSE# Indicates to the computer that you have finished with a file.

The statement *CAT can be used at any time. However before you can use any of the other file statements you have to OPEN the file. After you have opened a file you can read and write to it as much as you wish. When you have finished with the file you must close it.

An analogy may help to make one or two points clearer. The files are all kept in one room and your only method of communicating with them is via 5 telephone lines to 5 clerks. In addition there is a supervisor who knows which telephone line to use to communicate with the right clerk. It is the clerk's job to keep all the files tidy and you really have no idea how he looks after the files - nor does it matter, so long as he is efficient.

To return to our computer, suppose that we wish to create a file called "DRINKS" in which we list every drink we ever try. First of all we have to ask the supervisor to allocate a phone line and clerk to us. The statement.

X=OPENOUT "DRINKS"

will place the number of the channel (telephone line) allocated to the file into the variable X. Next we can ask the user for the names of the drinks using

INPUT "WHAT IS THE DRINK CALLED", D$

and then send the name (held in D$) to the clerk to be entered in the file. Notice how we have to use the variable X to ensure that it is entered in the correct file.

PRINT#X,D$

and then we could repeat this process until the user replied with the word STOP. The program would look like this

10 X=OPENOUT "DRINKS"

20 REPEAT

30 INPUT"What is the drink called ", D$

40 PRINT#X, D$

50 UNTIL D$="STOP"

60 CLOSE#X

When run the program will save the data on cassette (if one is connected).

>RUN

RECORD then RETURN

What is the drink called?WHISKY

What is the drink caLLed?PORT

What is the drink called?GIN

What is the drink caLLed?BEER

What is the drink caLLed!LAGER

What is the drink called?CIDER

What is the drink caLLed?TOMATO JUICE

What is the drink called?STOP

That has created a file called "DRINKS" which has been stored on cassette or disc. The program to read the file back in is also quite straightforward.

10 Y=OPENIN "DRINKS"

20 REPEAT

30 INPUT#Y , R$

40 PRINT R$

50 UNTIL R$="STOP"

60 CLOSE# Y

When this is run, if the tape is now played from a point just before the position where the data was recorded, the list will then appear on the screen.

>RUN

WHISKY

PORT

GIN

BEER

LAGER

CIDER

TOMATO JUICE

STOP

When using these programs on cassette you must be careful to position the tape (by "forward" and "rewind") so that the tape is in the right place. In the first program, where you record the data onto the tape you will have to find a piece of blank tape - preferably with the tape recorder counter set to 100 or 200 or 300 or some other round number. After you have finished the first program you have to rewind the tape to the start of the data file which was at, say, 200. It is just like trying to play a piece of music back that you have recorded on a cassette; you must rewind the cassette to the start of the piece. You may well know how difficult it is to find a particular record on a cassette and that is why it is so important to use the tape counter every time and to write down where each title is on the tape. Your index might look like

100 NAMES

200 DRINKS

300 SLIDES

400

For most applications that is all you will need to know about file handling and you will only use statements like these

*CAT

X=OPENIN "FILENAME"

X=OPENOUT "FILENAME"

PRINT#X,A,B$

INPUT#X,A,B$

CLOSE#X

"FILENAME" is the name of the file which normally consists of up to ten letters but see page 397 for more details.

A and B$ represent any (and as many) numeric and string variables as you wish to record.

X is a numeric variable used to remember the channel number allocated to the file number.

For more specialist applications a number of other functions and statements are provided. BGET# and BPUT# enable single characters to be input and output. They would be used for recording special data, for example from laboratory experiments.

EXT# and PTR# are mainly used with disc systems where

random access files are required. They cannot be used in a cassette environment.

EOF# enables a program to detect the end of the file when reading in data. It is normally used in the following way

10 Y=OPENIN "DRINKS"

20 REPEAT

30 INPUT#Y, A$

40 PRINT A$

50 UNTIL EOF#Y

60 CLOSE#Y

Telephone book program

One of the programs on the WELCOME cassette can be used to keep a personal telephone directory. Clearly it should be possible to save a copy of all your entries on to cassette and to load them back into the computer later. Several modifications must be made to the program to enable this to happen. These modifications are shown below. Once you have modified the program you can then save the corrected version with a new name, for example

SAVE "TELE2"

First, load the program. Don't RUN it, but type CTRL N then LIST to list the program in 'page mode'. To move down the program, press SHIFT. When you come to a page requiring one of the changes set out below, press ESCAPE and edit the line in the normal way.

Lines 210 to 280 omit final '

Add new lines:

282 PRINT" 9 - Load data from cassette"

284 PRINT" 0 - Save data to cassette"

Change line 290 to

290 SEL = -1

In line 300 change TAB(3,22) to TAB(3,23)

Line 330 change to

330 IF A<0 OR A>9 THEN 310

Change line 350 to

350 IF SEL> -1 THEN PRINT TAB(2,SEL+3 -10*(SEL=0));CHR$(&89);

Change line 360 to

360 PRINT TAB(2,A+3-10*(A=0));CHR$(&88)

;A;CHR$<&89)

Change line 380 to

380 IF SEL=-1 THEN 300

Change line 500 to

500 ON SEL +1 GOTO 505, 510,520,530,540 550,560,570,580,590

Newlines:

505 PROCSAVE:GOTO 200

590 PROCLOAD:GOTO 200

10000 DEF PROCLOAD

10005 PRINT TAB(0,16); "Play tape and press any key"

10007 Q=GET

10008 PRINT "Please wait"

10010 E%=OPENIN "DATA"

10020 INPUT#E%,X

10030 FOR 1%=1 TO X

10032 INPUT#E%,NAME$(I%),PHONE$(I%)

10034 PROCPACK (I%)

10036 NEXT

10040 CLOSE#E%

10050 ENDPROC

11000 DEF PROCSAVE

11005 PRINT TAB(0,16); "Please press ";

11010 E%=OPENOUT "DATA"

11015 PRINT '"Please wait"

11020 PRINT#E%,X

11030 FOR I%=1 TO X

11032 PRINT#E%,NAME$(I%),PHONE$(I%)

11036 NEXT

11040 CLOSE#E%

11050 ENDPROC

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001