PowerBASIC and MSBASIC both allow you to use the FIELD statement to define the fields for each record in a random access file. While previous versions of PB/CC did not support the FIELD statement, it is now supported with the FIELD statement. However, using fixed-length data types (such as a User-Defined Type, Union, fixed-length string or ASCIIZ string) with the GET and PUT statements is often more efficient.
Original Code:
OPEN "MYFILE.DAT" FOR RANDOM AS #1 LEN = 50
FIELD #1, 5 AS ID$
FIELD #1, 15 AS FIRSTNAME$
FIELD #1, 20 AS LASTNAME$
FIELD #1, 10 AS PHONE$
GET #1, 1 'get the first record
PRINT ID$
PRINT FIRSTNAME$
PRINT LASTNAME$
PRINT PHONE$
CLOSE #1
The field items need only be converted into a User-Defined Type, which can be used with the GET statement in PB/CC:
PB/CC Code:
TYPE Record
ID AS STRING * 5
FIRSTNAME AS STRING * 15
LASTNAME AS STRING * 20
PHONE AS STRING * 10
END TYPE
DIM r AS Record
OPEN "MYFILE.DAT" FOR RANDOM AS #1 LEN = 50
GET #1, 1, r 'get the first record
PRINT r.ID
PRINT r.FIRSTNAME
PRINT r.LASTNAME
PRINT r.PHONE
CLOSE #1
If you write a variable length string to a random access file with the PRINT statement in MSBASIC, you'll write more than the string. MSBASIC writes the length of the string as a two-byte Integer, which causes compatibility problems with existing code. PB/CC writes only the string and prints it exactly as it would to the console.
See Also