Disk File Input/Output

BASIC-SO

Opening and Closing a Random Disk File

As with sequential disk files, you must open random disk files before you can write to them or read from them, and close them when you're through. A random file isn't opened for either input or output, however; once it's open for random I/O, you can read from it or write to it interchangeably.

To open the file ":FI :PERSON" for random 110 and assign it file number 3:

OPEN "R", #3, ":F1 :PERSON"

Any of the parameters can be variables:

OPEN "R", F, FN$

This opens the file whose name is represented by the string variable FN$ for random I/O and assigns it the file number represented by the numeric variable F.

As with sequential I/O, the CLOSE statement can specify a file number (or numbers); CLOSE by itself closes all open files. The END statement and the NEW and EXIT commands close all open files; STOP does not.

Reading from a Random 1/0 File

You can retrieve data formatted and stored on disk with the GET statement. When entering the GET statement, you must specify the file you want to read. You can also specify the record you wish to read by entering the record number after the file number. If you don't specify the record number, BASIC-80 either reads the first record, if there has been no read operation since the file was opened, or reads the record that follows the last record read. To read the first record from file I:

GET #3,1

To read the next record:

GET #3

As described earlier (in "110 Buffers" and "Defining Random 110 Fields- FIELD), the GET statement reads 128 bytes into the I/O buffer assigned to the ran- dom file by the OPEN statement. The FIELD statement specifies which portions of the buffer are to be assigned to string variables (all data in random I/O files is stored as string data).

Assume, for example, you have a file written as a random liD file named :FI :PER- SON. The first 20 characters of each sector contains a name, and the next 9 characters contain a social security number. The following statements open the file, define which parts of the buffer correspond to variables, and read the first sector:

10OPEN "R", #3, ":F1 :PERSON"

20FIELD #3, 20 AS N$, 9 AS SS$

30GET #3,1

To print the name:

40 PRINT N$

5-6

Page 38
Image 38
Intel 9800758-02 manual Opening and Closing a Random Disk File, Reading from a Random 1/0 File, To read the next record