Random

Random Access Files
Run BASIC allows you to use files opened for random access. They can be read from and written to while open. They are read from with **Get** and **Gettrim**. They are written to with **Put**. They are opened with the **Open** statement, which requires an extra argument for the length of the record field. The structure of each record is specified with the **Field** statement. The variables in the **Field** can be either string or numeric, or a combination of both.

Opened files must be closed with the Close statement when they are no longer needed.

Here is the syntax for statements dealing with random files:

OPEN expr$ FOR random AS #handle len=n //- Open a file #handle using expr$ for random access, specifying length of field.// GET #handle, recNum //- Read record recNum from open random access file #handle// GETTRIM #handle, recNum //- Same as GET but strips leading and trailing spaces from field data// PUT #handle, recNum //- Write record recNum into open random access file #handle// FIELD #handle, length1 as varName, length2 as varName,. . .//- Set the length of the variables in each record.

Random Access files consist of RECORDS. The entire file is divided into many records. Each record has the same length. The length is specified when the file is opened with the **LEN** parameter. The example below opens a file called "members.dat" and sets the record length to 256:

code OPEN "members.dat" FOR RANDOM AS #1 LEN=256 code

Reading and writing to a file opened for random access is done one record at a time. A record may be read or written anywhere in the file. A record is read with either the **GET** statement, or with the **GETTRIM** statement. A record is written to the file with the **PUT** statement.

Each record is subdivided into "fields", each with a given length. The **FIELD** statement must be included after the OPEN statement to set up the size of each of the fields. Each field is designated by a variable name and given a specified length. When the lengths of all fields are added together, their sum must be equal to the length that was set with the LEN parameter in the OPEN statement. In the above case, the field lengths must total 256. A "$" character indicates that a field holds data that will be accessed as a string. If there is no "$" character, the field will be accessed as a number. The fields for "members.dat" might look like this:

code OPEN "members.dat" FOR RANDOM AS #1 LEN=256

FIELD #1,_ ' set up the fields for file opened as #1 90 AS Name$,_ ' 1st 90 bytes contains Name$, string 110 AS Address$,_ ' 2nd 110 bytes contains Address$, string 50 AS Rank$,_ ' 3rd 50 bytes contains Rank$, string 6 AS IDnumber ' 4th 6 bytes contains IDnumber, numeric code

The value after "LEN=" is 256, which is obtained by adding 90 + 110 + 50 + 6 or the total length of all the fields in FIELD#. The FIELD statement must follow the OPEN statement and must be used before trying to read or write data to the file with GET or PUT.

The **PUT** statement is used to write the data that is contained in the variables listed in the **FIELD** statement to the specified record number. Records in RANDOM files are numbered beginning at 1, not 0. If the length of a variable in a given field is shorter than the field length specified, blank spaces are added to pad it. If the length of the variable is larger, it will be truncated to the length specified in the FIELD statement.


 * GET** reads an entire record and fills the variables listed in the FIELD statement.


 * GETTRIM** retrieves the record in the same manner as GET, but it removes any blank leading or trailing spaces in the record.

Here's a small demo that opens a random file, declares the record field variables, fill those variables to write a record to the file, then reads that record.

code format="vbnet" 'Open a file for random access, with field length of 256. open "members.dat" for random as #1 len=256

'Declare the variables and their lengths for each record. 'Variables can be of either string or numeric type. FIELD #1,_ ' set up the fields for file opened as #1 90 AS Name$,_ ' 1st 90 bytes contains Name$, string 110 AS Address$,_ ' 2nd 110 bytes contains Address$, string 50 AS Rank$,_ ' 3rd 50 bytes contains Rank$, string 6 AS IDnumber ' 4th 6 bytes contains IDnumber, numeric

'Create a record by assigning values to the field variables. Name$ = "John Q. Public" Address$ = "456 Maple Street, Anytown, USA" Rank$ = "Expert Programmer" IDnumber = 99

'Add the record to the random file. This will be record 3. PUT #1, 3

'Now retrieve the info from record 3. GET #1,3

Print "Name is ";Name$ Print "Address is ";Address$ Print "Rank is ";Rank$ print "ID number is ";IDnumber

'Retrieve record without blank spaces: GETTRIM #1,3

Print "Name is ";Name$ Print "Address is ";Address$ Print "Rank is ";Rank$ print "ID number is ";IDnumber

'close the file close #1 end code