As mentioned previously, "random" is not a file type, but a method of organizing and accessing the information in a file. A file should be organized based on how the file will be used. Files in which the items are accessed in a random order should be organized as a random file.
BDAT files contain fixed length records that can be accessed by record number. The record number is specified after the I/O path variable. The record length is specified when creating the file. The record size must be set to accommodate the largest data item.
10 REM Random file example (BDAT File)
20 DIM C$[56]
30 CREATE BDAT "customer.dat",100,60
40 ASSIGN @C TO "customer.dat"
50 CLEAR SCREEN
60 LOOP
70 DISP "A(dd, D(elete, S(how, Q(uit and take a vacation"
80 ON KBD GOTO Inkey
90 LOOP !endlessly until a key is pressed
100 OUTPUT CRT;TIME$(TIMEDATE);CHR$(13);
110 END LOOP
120 Inkey: K$=KBD$
130 OFF KBD
140 OUTPUT CRT
150 SELECT UPC$(K$)
160 CASE "A"
170 PRINT "Add:"
180 INPUT "Customer Number? ",C
190 INPUT "Information? ",C$
200 OUTPUT @C,C;C$
210 PRINT "Customer number #";C;"added"
220 CASE "D"
230 PRINT "Delete:"
240 INPUT "Customer Number? ",C
250 OUTPUT @C,C;"DELETED"
260 PRINT "Customer number #";C;"deleted"
270 CASE "S"
280 PRINT "Show:"
290 INPUT "Customer Number? ",C
300 ENTER @C,C;C$
310 PRINT "Customer number #";C;":",C$
320 CASE "Q"
330 PRINT "Thank you for using HTBasic!"
340 PRINT "Have a nice vacation."
350 DISP ! clear display line
360 STOP
370 CASE ELSE
380 PRINT CHR$(7);! ring the bell for a bad command
390 END SELECT
400 END LOOP
410 END
This example, of course, is not a complete application. But it does show the important aspects of random file use, as well as some user interface techniques. Note that the record size was declared to be 60. The length of each record can never exceed this, since each record consists of C$ (which can never be longer than 56 characters) plus the four byte length of C$ which we know will be included in the file since we are using a BDAT file with FORMAT OFF (the default).
regular files do not have a physical record length, but you can still use a logical record length. The record number actually specifies the exact byte position in the file. The first byte is at position 1. To access a logical record, the byte position must be calculated based on the logical record length. The following example has the same capabilities as the previous program, but uses a regular file.
10 REM Random file example (regular file)
20 DIM C$[58]
30 Length=60 ! 58 Character string + CR/LF
40 CREATE "customer.dat",100
50 ASSIGN @C TO "customer.dat";FORMAT ON
60 CLEAR SCREEN
70 LOOP
80 DISP "A(dd, D(elete, S(how, Q(uit and take a vacation"
90 ON KBD GOTO Inkey
100 LOOP !endlessly until a key is pressed
110 OUTPUT CRT;TIME$(TIMEDATE);CHR$(13);
120 END LOOP
130 Inkey: K$=KBD$
140 OFF KBD
150 OUTPUT CRT
160 SELECT UPC$(K$)
170 CASE "A"
180 PRINT "Add:"
190 INPUT "Customer Number? ",C
200 INPUT "Information? ",C$
210 OUTPUT @C,(C-1)*Length+1;C$
220 PRINT "Customer number #";C;"added"
230 CASE "D"
240 PRINT "Delete:"
250 INPUT "Customer Number? ",C
260 OUTPUT @C,(C-1)*Length+1;"DELETED"
270 PRINT "Customer number #";C;"deleted"
280 CASE "S"
290 PRINT "Show:"
300 INPUT "Customer Number? ",C
310 ENTER @C,(C-1)*Length+1;C$
320 PRINT "Customer number #";C;":",C$
330 CASE "Q"
340 PRINT "Thank you for using HTBasic!"
350 PRINT "Have a nice vacation."
360 DISP ! clear display line
370 STOP
380 CASE ELSE
390 PRINT CHR$(7);! ring the bell for a bad command
400 END SELECT
410 END LOOP
420 END
BDAT files give an error if a single OUTPUT is too long for the record length (unless the record length is one). It is the programmer’s job to make sure that record overflow does not occur.