Liberty BASIC Help Online

Random Access Files
OPEN filename FOR RANDOM AS #handle LEN=n
To access a file in random access mode, it must be opened with the OPEN statement.  When it is no longer needed, and before the program ends, it must be closed with the CLOSE statement.  See also: FiledialogFile Operations, Path and Filename , PUT, FIELD, GET, GETTRIM
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:
OPEN "members.dat" FOR RANDOM AS #1 LEN=256
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.  These statements are explained in more detail below.
FIELD 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:
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
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. 
PUT #handle, number
This 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.  To add this data as RECORD number 3 to the file referenced above:
Name$ = "John Q. Public"
Address$ = "456 Maple Street, Anytown, USA"
Rank$ = "Expert Programmer"
IDnumber = 99
PUT #1, 3
GET #handle, number
This statement reads an entire record and fills the variables listed in the FIELD statement.
GET #1,3
Print Name$ would produce "John Q. Public                                             "
Print Address$ would produce "456 Maple Street, Anytown, USA                             "
Print Rank$ would produce "Expert Programmer                "
print IDnumber would produce "99"
and so on.
GETTRIM #handle, number
This command retrieves the record in the same manner as GET, but it removes any blank leading or trailing spaces in the record:
Print Name$ would produce "John Q. Public"
' no blank spaces are included

Copyright (C) 2003 Shoptalk Systems
Liberty BASIC -