Random access file management
Field #n, count As set$ [, count As set$...]
Field #n, count At(x) [, count At(x), ...]
n:integer expression; channel number
count:integer expression
set$:svar, but not an array variable
x:addr
RANDOM ACCESS files are composed of records and fields. A record is a collection of data, for example an address. A record contains a mixture of fields (the record Address can, for example, be divided into fields: Name, Street, Postcode, and City). Both records and fields have a set size.
Field #n divides records into fields. n is the channel number (from 0 to 511) of a file previously opened with Open. The integer count defines the corresponding field length. The string variable set$ always refers to one field in a record. If a record is divided into several fields, each must be separated with a comma (count As set$). The sum of individual field sizes must be equal to the length of the record. To save individual fields with length given in count, the commands Lset, Rset and Mid$ should be used. Using Field At numerical variables can be written to an R-file (random access) without having to convert them to strings. The pointer to numerical variables which are to be saved is given in brackets after At and the number of bytes to read from this address is given before At. A mixture of As and At is allowed.
The Field command can span across several program lines.
The Field statement can use a TYPE variable a. The address of the TYPE variable is used.
Field #1, Len(a) At V:a
OpenW 1
// a simple declaration
Global i%, name$, town$, zip%, ss$, x%
// to open the file
// Open App.Path & "\addresses.dat" for Random As # 1, Len = 64
// Field construct
// Field #1,24 As name$,24 As ss$,4 At (V:zip&)
// Field #1, 12 As town$
//
// or direct with Option Base
Open App.Path & "\addresses.dat" for Random Based 1 As # 1, Len = 64
Field # 1, 24 As name$
Field # 1, 24 As ss$
Field # 1, 4 At (V:zip%)
Field # 1, 12 As town$
//
For i% = 1 To 5
Lset name$ = "NAME: " + Str$(i%)
Lset ss$ = "STREET: " + Str$(i%)
zip% = i%
Lset town$ = "TOWN: " + Str$(i%)
Put # 1, i%
Next
For i% = 1 To 5
Get # 1, i%
Print "record_number : "; Str$(i%, 3)
Print name$
Print ss$
Print zip%
Print town$
Next
Close # 1
Kill App.Path & "\addresses.dat" // Tidy up line
{Created by Sjouke Hamstra; Last updated: 26/11/2023 by James Gaite}