home *** CD-ROM | disk | FTP | other *** search
- Using Random Files In BASIC
-
- Thomas Mack
- Capital PC Users Group
-
- For those with a limited amount of
- disk space on their IBM PC's,
- "random" files may offer a partial
- solution. Appendix B-8 of the IBM
- BASIC manual states, "In many cases
- random files require less space on
- diskette than sequential files."
-
- While IBM has documented that
- "creating and accessing random files
- requires more program steps than
- sequential files," IBM has not
- documented the differences between
- the DOS 1.1 BASIC interpreter, the
- DOS 2.0 BASIC interpreter, and the
- BASIC compiler. I shall attempt to
- do that in this article so that
- others may not have to endure the
- discovery of these differences.
-
- Random files have records that may
- be of any length up to 32,767 bytes.
- The size of a record is not related
- to the size of a sector on the
- diskette (512 bytes). BASIC
- automatically uses all 512 bytes in
- a sector for information storage.
- It does this by both blocking
- records and spanning sector
- boundaries (that is, part of a
- record may be at the end of one
- sector and the other part at the
- beginning of the next sector).
-
- It is important to understand how
- the BASIC interpreter and the BASIC
- compiler determine the buffer size
- for random files.
-
- p Under the BASIC interpreter, the
- "/S:bsize" option of the BASIC
- command sets the buffer size for use
- with random files. The record
- length parameter on the OPEN
- statement may not exceed this value.
- The default buffer size is 128
- bytes. The maximum value you may
- enter is 32,767 bytes. The BASIC
- compiler manual states: " ...there
- is no equivalent in the compiler for
- /S: option on the BASIC command used
- to start Disk and Advanced BASIC
- from DOS. The buffer size for
- random files is determined by the
- LEN= parameter on the OPEN
- statement; there is no maximum
- record length."
-
- When using random files in BASIC
- programs, it is important to know
- that the user has the responsibility
- for locating the last record in the
- random file. This is especially
- true when the file is not a fixed
- size of does not have a fixed number
- of records.
-
- Under DOS 2.0's BASIC interpreter,
- the LOF function returns the number
- of bytes allocated to the file on a
- diskette if it was created by the
- BASIC interpreter under DOS 2.0!
- The following program, RANDOM1.BAS,
- will write 10 records, each 64 bytes
- long, onto a diskette and use a
- total of 640 bytes under DOS 2.0's
- BASIC interpreter:
-
- 1000 DEFINT A-Z
- 1010 CALLER$="B:CALLERS"
- 1020 KILL CALLERS$
- 1030 FOR I=1 TO 10
- 1040 CLOSE 2
- 1050 OPEN "R",2,CALLERS$,64:FIELD
- 2, 64 AS CALRINFO$
- 1060 NAMSAV$="LOGICAL RECORD
- NUMBER"+STR$(I)
- 1070 LSET CALRINFO$=
- LEFT$(NAMSAVE$,64)
- 1075 ILOF=(LOF(2)/64)+1
- 1080 PUT #2,(LOF(2)/64)+1
- 1090 PRINT I,NAMSAV$,ILOF
- 1100 NEXT
- 1110 STOP
-
- The 640-byte file size is constant
- (10 records x 64 bytes each = 640
- bytes) independent of what you set
- the random file buffer size to when
- using the /S:bsize option for the
- DOS 2.0 BASIC interpreter command.
-
- For diskette files, LOF will return
- a multiple of 128. For example, if
- the actual data in the file is 257
- bytes, the number 384 will be
- returned. Because of this,
- RANDOM1.BAS (under DOS 1.1 or when
- compiled by the BASIC compiler) will
- use 1280 bytes of disk space to
- write the ten 64-byte records. This
- also will occur no matter what you
- specify for the /S:bsize parameter
- for the interpreter of the LEN=
- parameter on the OPEN for the
- compiler.
-
- What actually happens under DOS
- 1.1's BASIC interpreter and version
- 1.00 of the BASIC compiler is that
- random files are written in
- 128-byte-long physical records to
- the diskette. This occurs
- independently of what the actual
- logical record size is. Therefore,
- in order to avoid any unexpected
- consumption of disk space by random
- files that are not fixed in length
- or have a fixed number of records,
- the user must always determine the
- last logical record in the random
- file.
-
- Fortunately a technique to do this
- is illustrated by IBM in Appendix B
- of the BASIC interpreter's manual
- under "Performance Hints". I will
- discuss the technique of using a
- dummy "end-of-file" indicator to
- locate the last record in a random
- file. The following program, called
- RANDOM2.BAS, illustrates this
- technique as it writes 10 records,
- each 64 bytes long, onto a diskette
- along with an eleventh record (the
- end-of-file record) and uses a total
- of 704 bytes under DOS 2.0's BASIC
- interpreter:
-
- 1000 DEFINT A-Z
- 1010 CALLERS$="B:CALLERS"
- 1015 LAST$="END-OF-FILE"
- 1020 KILL CALLERS$
- 1030 FOR I=1 TO 10
- 1040 CLOSE 2
- 1050 OPEN "R",2,CALLER$,64:FIELD
- 2, 64 AS CALRINFO$
- 1060 NAMSAV$="LOGICAL RECORD
- NUMBER"=STR$(I)
- 1065 GOSUB 1115
- 1070 LSET CALRINFO$=NAMSAV$
- 1080 PUT #2,IREC
- 1085 LSET CALRINFO$=LAST$
- 1086 PUT #2,LOC(2)+1
- 1090 PRINT I,NAMSAV$,LOC(2)
- 1100 NEXT
- 1110 STOP
- 1115 IF LOF(2)<1 THEN
- IREC=1:RETURN
- 1125 IREC=(LOF(2)/64)
- 1135 GET #2,IREC
- 1145 TEST$=LEFT$(CALRINFO$,11): IF
- TEST$=LAST$ THEN RETURN
- 1155 IREC=IREC-1
- 1165 GOTO 1135
-
- Under DOS 1.1's BASIC interpreter
- and version 1.00 of the BASIC
- compiler, this program also writes
- 10 records plus an eleventh
- end-of-file record and uses 768
- bytes. This is because random files
- are written in 128-byte physical
- records and the eleventh record,
- while only using 64 bytes of the
- last 128-byte physical record,
- requires a full 128 bytes of disk
- storage to be allocated to it.
- Under DOS 1.1's BASIC interpreter or
- version 1.00 of the BASIC compiler,
- the file consists of 6 physical
- records, each 128 bytes long, to
- hold the 11 logical records.
-
-
- Therefore, if you are writing
- programs that use random files that
- are not fixed (i.e., in length or
- the number of records) and that may
- be compiled and/or may run under DOS
- 1.1 or DOS 2.0, the "dummy
- end-of-file technique" will allow
- you to make your application
- transportable.