Chapter six: File handling

Scan of page 37 Scan of page 38 Scan of page 39 Scan of page 40 Scan of page 41 Scan of page 42 Scan of page 43 Scan of page 44

Introduction

Data may be presented to programs in files instead of via DATA statements or INPUT statements. This allows the information to be accessible by more than one program. A file is a linear arrangement of data external to all BASIC programs which can be read and amended by BASIC statements.

Two kinds of file can be accessed by the BASIC system. These are known as terminal format files and internal format files.

Terminal format files

These are made up of variable length records each of which resembles a line of printed characters. They are accessed serially and can be listed on a terminal.

Statements and commands which process terminal format files

The OPEN command

This command is used outside the program to create a file and takes the form:

OPEN name, recs, access

with all parameters optional except name.

e,g,:

OPEN TFILE,50,SHARE

will open a terminal format file called TFILE with fifty records which may be accessed by all users.

The other access modes are:

USER may only be accessed by the user who created it.
READ may be read by other users but not amended by other users.
WRITE equivalent to SHARE. The file may be read and written to by any user.

The default number of records is 100. The filename may be up to six alphanumeric characters with the first alphabetic. When the file is created by OPEN it contains no data.

The KILL command

When a file has been opened it remains on the disc until it is deleted by the KILL command, which has the format

KILL filename

The EXTEND command

If it is found that more records are needed on a file than the number specified in the OPEN command, the file may be extended.

e.g.:

EXTEND TFILE,24

will put space for twenty four extra records in TFILE.

The GET command

A terminal format file can be brought into store from the disc by means of the GET command, in the same way as a BASIC program

e.g.:

GET TFILE
GET TFILE,USER

The LIST command

The contents of a terminal format file can be listed in the same way as a BASIC program. The formats of the LIST command are the same as those described in Chapter two, allowing a whole file or specific records to be listed. The record number, starting from one, is output, followed by the data in the record.

e.g.:

GET TFILE
LIST 10,12
TFILE
10 ΤΕΝΤΗ LΙΝΕ
11 NO. ELEVEN
12 12TH LINE

The DELETE command

Similarily, records can be deleted in exactly the same way that lines are deleted from a BASIC program. See Chapter two for formats.

e.g.:

GET TFILE
DELETE 11
LIST
10 ΤΕΝΤΗ LΙΝΕ
12 12TH LINE

To add new data the procedure is to type the record number followed by the data. This will be incorporated into the file.

BEWARE DELETE with no parameters deletes the whole file.

The FILE statement

This is used to link a previously created data file to the program. It assigns to the file a channel number between 1 and 6, and all input and output statements refer to the channel number rather than to the file name. This number is preceded by a #. In the following example a file is opened and the first line of the BASIC program then assigns the file to a channel number.

e.g.:

OPEN TFILE2,5,SHARE
10 FILE #1: "TFILE2"

As the access mode in the OPEN command is SHARE, this file could be read from another username. If TFILE2had been created in username USERO1, it could be assigned to a program within another username in this way:

20 FILE #6: "TFILE2, USER01"

The channel number can be simply a number, as in the examples above, or it can be expressed as a numeric variable or arithmetic combination of numeric variables.

e.g.:

15 FΙLΕ #Α + Β:"ΝΑΜΕ"

where it would be assigned to channel 4 if A + B = 4.

The file statement is also used to end channel associations to free the channel for use by another file. If channel 2 were to be freed the following statement would be used:

50 FILE:#2: "*"

The PRINT statement

Terminal format files are intended to resemble lines of print as they would appear on a terminal, thus data can be written to the file using the PRINT statement. In the following example the data is read by the program from DATA statements and then printed into the file.

e.g.:

OPEN TFILE3
10 FILE #1: "TFILE3"
20 FΟR T = 1 ΤΟ 3
30 READ A, B, C$
40 PRINT #1: T;A*B,A;" ";B,C$&"HORSE"
50 ΝΕΧΤ T
60 DATA 2,3, PACK, 1,6, CART, 4,2, CLOTHES
70 END

If the file were listed, the following would be output:

1  6          2 3            ΡΑCΚΗΟRSΕ
2  6          1 6            CARTHORSE
3  8          4 2            CLOTHESHORSE

The comma separator causes spacing across the line and the semi-colon separator prints elements adjacent to each other. The maximum line length for a terminal format file is initially 72 characters, but this can be reduced or increased up to 124 by the MARGIN statement described in Chapter five. When MARGIN refers to a file it has the following format.

e.g.:

   10 MARGIN #2:48
Or 10 MARGIN #X;Y+3

If the current line length is not known the MRG Function can be used to find it.

e.g.:

30 Y = MRG(#2)

will set Y equal to the line length of the file occupying channel 2.

The formatted print statement PRINT USING, described in Chapter five, may also be used for writing to terminal format files.

e.g.:

40 PRINT #2: USING "£-###.##": X

or if AŞ = “£-###.##”

40 PRINT #2: USING A$:X

This will store £▽▽12.02 if X = 12.02

The INPUT and LINPUT statements

Data can be read from terminal format files by means of these two statements. INPUT will enable the data from one PRINT statement to be made available in a series of numeric or string variables in the same format in which it was originally stored using PRINT, with each variable separated by commas. LINPUT will present to the program all the characters written in one print statement as a single string, so that

e.g.:

20 LINPUT #1:A$

will deposit the next line of data from the terminal format file into the string variable A$. Access to these files is always serial.

Examples

If a terminal format file contains the following data:

1 ONE, TWO
2 THREE, FOUR

the use of INPUT and LINPUT will result in the formats shown below.

10 FILE #1: "FILE01"
20 INPUT #1: A$,B$
30 LINPUT #1: C$
40 PRINT A$
50 PRINT B$
60 PRINT C$
70 END
RUN
ONE
TWO
THREE, FOUR
LINE 70 DONE

The IF MORE and IF END statements

These two statements are used to detect the end of the data in a file

e.g.:

100 IF MORE#1 GOTO 20

If the pointer for the file has more data following it, the program will go back to statement line 20.

IF END is the inverse of IF MORE.

The SCRATCH statement

This is used to delete all the data held on a file without erasing the file itself. The pointer is set to the start of the file occupying the specified channel.

e.g.:

50 SCRΑΤCΗ #1

The RESET statement

This is used to reset the record pointer to the beginning of the file occupying the specified channel so that the data may be accessed again

e.g.:

40 RESET #2

The RESTORE statement is synonymous, but by convention it is used to reset the pointer in program DATA statements, whereas RESET is used when referring to files.

File handling functions

The LOC function gives the current record number. It can be used for example to count the number of lines input to or output from a file.

e.g.:

40 IF LOC(#1) = 20 GOTO 100

will pass control to line 100 when the 20th record is accessed.

The LOF function will give the number of records in the file as stated in the OPEN command, modified by any EXTEND commands.

e.g.:

50 PRINT LOF(#6);

The TYP function can be used to identify the type of the next item. It is set to 1 if the item is numeric, 2 if string, 3 if end of record, 4 if end of file where end of file is determined by the size given in the OPEN command and any further extension.

TYP can be used as shown

e.g.:

10 FİLE #1: "TFILE"
20 LET F = TYP(#1)
30 PRINT "TYP=";F;
40 ON F GOSUB 100,200,300,320
50  ΤΟ 20
100 INPUT #1:X,
110 PRINT "NUMBER = ";X
120 RETURN
200 INPUT #1:X£,
210 PRINT "" STRING = ";X£
220 RETURN
300 PRINT " END OF RECORD";LOC(#1)
305 RESET #1,LOC (#1) + 1
310 RETURN
320 END

RUN
ΤΥΡ = 1 ΝUΜΒΕR = 14
TYP = 1 NUMBER = -23
TYP = 2 STRING = ABCD
TYP = 3 END OF RECORD 1
TYP = 1 NUMBER = 3
TYP = 2 STRING = EF
TYP = 2 STRING = GHIJK
TYP = 1 NUMBER = 4
ΤΥΡ = END OF RECORD 2
LINE 30 NUMBER NOT PREVIOUSLY WRITTEN TO THIS FILE.

Internal format files

These are made up of fixed format, fixed length records. Their fields can be in binary or character form and access to the records can be serial or direct.

Statements and commands which process internal format files

Some of the commands and statements which act on terminal format files also apply to internal format files. It may be assumed that those mentioned here are identical for both files unless variations are mentioned in this section, and that those not mentioned may not be used with internal format files.

The OPEN command

The format for an internal format file is OPEN name (record format), recs, access. When creating an internal format file, the record format must be specified. This is done in the following way:

N = 1 numeric value
Sn = 1 string value with n characters
mN = m numeric values
mSn = m string values with n characters each.

e.g.:

OPEN IFILE(3N,2S4,2N,S20,N),30, WRITE

Where the record format is three numeric fields, two four-character strings, two numeric fields, a twenty-character string then one numeric field. The data which is input to the file must then correspond exactly with the original format. If a user forgets the format he gave his file on opening, a program called %FORMAT exists to give this information. It requests the user to type in the name of the file. It is necessary only totype %FORMAT to GET and RUN this program.

e.g.:

%FΟRΜΑΤ
SUPPLY FILENAME? IFILE
30 RECORDS FORMAT 3N, 2S4, 2N, S20, N

Note: If the format definition ends with a comma then flow from one record to the next is allowed for the file; this means that read and write operations on the file which attempt to go beyond the end of a record pass onto the next record. If the comma is not present this flow is not allowed.

e.g.:

OPEN IFILE (10N,)

The number of records given will be the number specified on opening - if only fifteen records have been written to the file, %FORMAT will still give thirty as number of records.

Internal format files can be KILLed and EXTENDed. The FILE statement is identical for terminal format and internal format files.

The WRITE statement

Data can be written to an internal format file either serially or record by record.

e.g.:

WRITE #1: A,B,C,D$

will write the three numbers and the character string to the next record of the file 1. Clearly this activity will fail if the file has not been opened with a format like 3N, 1S4. If the string D$ contains more than four characters in the example given just the left most four characters will be stored on the file.

Writing will normally start from record 1 on the file.

e.g.:

WRITE #1, 12: A,B,C,D$

will write data to the 12th record on the file.

The format:

WRITE #X,Y: A,B,C,D$

is also permissible where X and Y are channel and record numbers respectively. To write to the end of the file:

WRITE #1, LOF(#1): A,B,C,D$

will direct the output to the last record.

The format:

WRITE #1, LOC(#1) + 2: A,B,C,D$

will direct the output to two records further on from the current file position.

The READ statement

The READ statement is used to move data from an internal format file into a program. Its format is very similar to the WRITE statement

e.g.:

READ #1: A,B,C,D$

will read the next record from the file on channel 1 into the numeric variables A, B and C and the string variable D$.

Specific records can be directly accessed.

e.g.:

READ #1,7: A,B,C,D$

will read the 7th record from file #1.

Numeric variables can be used instead of numeric constants for specifying the channel and record number

e.g. if X = 1 and Y = 3 and Z = 4:

READ #X,Y+Z: A,B,C,D$

will read the 7th record from file 1.

The standard file handling function can be used

e.g.:

READ #2,LOC(#1): A,D$

would ensure that if the 7th record had just been processed on file 1, the 7th record would be READ on file 2.

LET X = LOF(#2)

would give X the value set up by the command OPEN, modified where appropriate by any subsequent EXTEND commands. If preceding programs have not written to every record on the file the READ action will be terminated by the message

"LINE 40 NUMBER NOT PREVIOUSLY WRITTEN TO THIS RECORD"

where line 40 of the program containsthe READ statement. The program can be restarted if necessary using the CONTINUE command but it should be noted that all channel associations will, by then, be lost.

The IF MORE and IF END statements

These two statements are used in the same way for internal format files as terminal format files. They will monitor the end of file as established by the OPEN command, rather than the end of data.

Statements and commands which can be used with all types of files

KILL, EXTEND, FILE, IF MORE, IF END, SCRATCH, RESET are all identical for both types of files.

CΗΑΙΝ

All the files in a program will normally be closed at the end of a run so that the channel associations set up in the FILE statement disappear. The statement

300 CHAIN "PROG" WITH #2, #1

will enable the program PROG to be run immediately after the current program whilst retaining the channel associations of the current program. All file associations also end on break in or other halts to program execution so that the only file handling statements that can be used with immediate execute (described in Chapter eight) must be preceded by a FILE statement.