BB___File_Type_Random

=Random Access Files= toc

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
> NOTE: The field statement must follow the open statement.
 * 1) open //fileName// for random as #1 len=//n//

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.

include component="page" page="BB___File_Type_Table" editable="1" wrap="1"

Example
code format="lb" ' 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 code

Useful Procedures
code format="lb" Place a useful function using this keyword here code