File Type Random

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

Random access files consist of records which are divided into "fields". Records are accessed one at a time. A record can be accessed for reading or writing anywhere in the file. Random access files must be OPENed to be used and they must be CLOSEd when finished.
See PUT, FIELD and GET.

Syntax

  1. open fileName for random as #1 len=n
NOTE: The field statement must directly follow the open statement.

Hints

  1. Records are numbered beginning at 1, not 0.
  2. All records are the same length which is specified with the LEN parameter when the file is OPENed.
  3. There is a limit on record's length: you cannot set LEN parameter to more then 65536.
  4. Each record is sub-divided into fields and all records consist of the same fields, whether the field is used or not. The total length of all fields equals the LEN argument of the OPEN command.
  5. A record is written to the file using the PUT statement.
  6. A record is read from the file using the GET statement.
  7. If the data in a field does not completely fill the field, the remainder of the field is padded with spaces. Using the TRIM$() function will allow removing the additional spaces from the data.
  8. The "padding" assures that all records are of the same length, regardless of the data they may contain.
  9. Records are stored on disk one after the other, with no new line characters. This makes it difficult to view a Random access file in Notepad, or other text editor. The LEN parameter is critical to retrieve the record correctly.

Below find a table of the relation between file content type and preferred access mode.

File Access Modes Best Practice
Content Mime Type Typical Extension Access Mode Reading Writing Open Mode
Plain Text text/plain ini, txt, log Sequential INPUT PRINT read or write
Random Access Random GET PUT read and write (*)
Anything else bmp, gif, doc, etc. Binary INPUT$() PRINT read and write (*)

(*) The file must be WRITE-able. So attempting to open a read-only file (or file from read-only media, like CD) for RANDOM or BINARY will fail.


Example

' Define a Random access file
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

' Add data to the file
Name$ = "John Q. Public"
Address$ = "456 Maple Street, Anytown, USA"
Rank$ = "Expert Programmer"
IDnumber = 99

' Data is written to the file using the PUT statement.
' Here, we will write to record #3.
PUT #1, 3

' To retrieve data in record #3, use the GET statement.
' 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"

close #1
end

Useful Procedures

' Place a useful function using this keyword here