home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MISC / TNH_PC.ZIP / MACK.NL < prev    next >
Encoding:
Text File  |  1987-01-14  |  6.0 KB  |  210 lines

  1. Using Random Files In BASIC
  2.  
  3.              Thomas Mack
  4.         Capital PC Users Group
  5.  
  6. For those with a limited amount of
  7. disk space on their IBM PC's,
  8. "random" files may offer a partial
  9. solution.  Appendix B-8 of the IBM
  10. BASIC manual states, "In many cases
  11. random files require less space on
  12. diskette than sequential files."
  13.  
  14. While IBM has documented that
  15. "creating and accessing random files
  16. requires more program steps than
  17. sequential files," IBM has not
  18. documented the differences between
  19. the DOS 1.1 BASIC interpreter, the
  20. DOS 2.0 BASIC interpreter, and the
  21. BASIC compiler.  I shall attempt to
  22. do that in this article so that
  23. others may not have to endure the
  24. discovery of these differences.
  25.  
  26. Random files have records that may
  27. be of any length up to 32,767 bytes.
  28. The size of a record is not related
  29. to the size of a sector on the
  30. diskette (512 bytes).  BASIC
  31. automatically uses all 512 bytes in
  32. a sector for information storage.
  33. It does this by both blocking
  34. records and spanning sector
  35. boundaries (that is, part of a
  36. record may be at the end of one
  37. sector and the other part at the
  38. beginning of the next sector).
  39.  
  40. It is important to understand how
  41. the BASIC interpreter and the BASIC
  42. compiler determine the buffer size
  43. for random files.
  44.  
  45. pUnder the BASIC interpreter, the
  46. "/S:bsize" option of the BASIC
  47. command sets the buffer size for use
  48. with random files.  The record
  49. length parameter on the OPEN
  50. statement may not exceed this value.
  51. The default buffer size is 128
  52. bytes.  The maximum value you may
  53. enter is 32,767 bytes.  The BASIC
  54. compiler manual states: " ...there
  55. is no equivalent in the compiler for
  56. /S: option on the BASIC command used
  57. to start Disk and Advanced BASIC
  58. from DOS.  The buffer size for
  59. random files is determined by the
  60. LEN= parameter on the OPEN
  61. statement; there is no maximum
  62. record length."
  63.  
  64. When using random files in BASIC
  65. programs, it is important to know
  66. that the user has the responsibility
  67. for locating the last record in the
  68. random file.  This is especially
  69. true when the file is not a fixed
  70. size of does not have a fixed number
  71. of records.
  72.  
  73. Under DOS 2.0's BASIC interpreter,
  74. the LOF function returns the number
  75. of bytes allocated to the file on a
  76. diskette if it was created by the
  77. BASIC interpreter under DOS 2.0!
  78. The following program, RANDOM1.BAS,
  79. will write 10 records, each 64 bytes
  80. long, onto a diskette and use a
  81. total of 640 bytes under DOS 2.0's
  82. BASIC interpreter:
  83.  
  84. 1000  DEFINT A-Z
  85. 1010  CALLER$="B:CALLERS"
  86. 1020  KILL CALLERS$
  87. 1030  FOR I=1 TO 10
  88. 1040  CLOSE 2
  89. 1050  OPEN "R",2,CALLERS$,64:FIELD
  90.       2, 64 AS CALRINFO$
  91. 1060  NAMSAV$="LOGICAL RECORD
  92.       NUMBER"+STR$(I)
  93. 1070  LSET CALRINFO$=
  94.       LEFT$(NAMSAVE$,64)
  95. 1075  ILOF=(LOF(2)/64)+1
  96. 1080  PUT #2,(LOF(2)/64)+1
  97. 1090  PRINT I,NAMSAV$,ILOF
  98. 1100  NEXT
  99. 1110  STOP
  100.  
  101. The 640-byte file size is constant
  102. (10 records x 64 bytes each = 640
  103. bytes) independent of what you set
  104. the random file buffer size to when
  105. using the /S:bsize option for the
  106. DOS 2.0 BASIC interpreter command.
  107.  
  108. For diskette files, LOF will return
  109. a multiple of 128.  For example, if
  110. the actual data in the file is 257
  111. bytes, the number 384 will be
  112. returned.  Because of this,
  113. RANDOM1.BAS (under DOS 1.1 or when
  114. compiled by the BASIC compiler) will
  115. use 1280 bytes of disk space to
  116. write the ten 64-byte records.  This
  117. also will occur no matter what you
  118. specify for the /S:bsize parameter
  119. for the interpreter of the LEN=
  120. parameter on the OPEN for the
  121. compiler.
  122.  
  123. What actually happens under DOS
  124. 1.1's BASIC interpreter and version
  125. 1.00 of the BASIC compiler is that
  126. random files are written in
  127. 128-byte-long physical records to
  128. the diskette.  This occurs
  129. independently of what the actual
  130. logical record size is.  Therefore,
  131. in order to avoid any unexpected
  132. consumption of disk space by random
  133. files that are not fixed in length
  134. or have a fixed number of records,
  135. the user must always determine the
  136. last logical record in the random
  137. file.
  138.  
  139. Fortunately a technique to do this
  140. is illustrated by IBM in Appendix B
  141. of the BASIC interpreter's manual
  142. under "Performance Hints". I will
  143. discuss the technique of using a
  144. dummy "end-of-file" indicator to
  145. locate the last record in a random
  146. file.  The following program, called
  147. RANDOM2.BAS, illustrates this
  148. technique as it writes 10 records,
  149. each 64 bytes long, onto a diskette
  150. along with an eleventh record (the
  151. end-of-file record) and uses a total
  152. of 704 bytes under DOS 2.0's BASIC
  153. interpreter:
  154.  
  155. 1000  DEFINT A-Z
  156. 1010  CALLERS$="B:CALLERS"
  157. 1015  LAST$="END-OF-FILE"
  158. 1020  KILL CALLERS$
  159. 1030  FOR I=1 TO 10
  160. 1040  CLOSE 2
  161. 1050  OPEN "R",2,CALLER$,64:FIELD
  162.       2, 64 AS CALRINFO$
  163. 1060  NAMSAV$="LOGICAL RECORD
  164.       NUMBER"=STR$(I)
  165. 1065  GOSUB 1115
  166. 1070  LSET CALRINFO$=NAMSAV$
  167. 1080  PUT #2,IREC
  168. 1085  LSET CALRINFO$=LAST$
  169. 1086  PUT #2,LOC(2)+1
  170. 1090  PRINT I,NAMSAV$,LOC(2)
  171. 1100  NEXT
  172. 1110  STOP
  173. 1115  IF LOF(2)<1 THEN
  174.       IREC=1:RETURN
  175. 1125  IREC=(LOF(2)/64)
  176. 1135  GET #2,IREC
  177. 1145  TEST$=LEFT$(CALRINFO$,11): IF
  178.       TEST$=LAST$ THEN RETURN
  179. 1155  IREC=IREC-1
  180. 1165  GOTO 1135
  181.  
  182. Under DOS 1.1's BASIC interpreter
  183. and version 1.00 of the BASIC
  184. compiler, this program also writes
  185. 10 records plus an eleventh
  186. end-of-file record and uses 768
  187. bytes. This is because random files
  188. are written in 128-byte physical
  189. records and the eleventh record,
  190. while only using 64 bytes of the
  191. last 128-byte physical record,
  192. requires a full 128 bytes of disk
  193. storage to be allocated to it.
  194. Under DOS 1.1's BASIC interpreter or
  195. version 1.00 of the BASIC compiler,
  196. the file consists of 6 physical
  197. records, each 128 bytes long, to
  198. hold the 11 logical records.
  199.  
  200.  
  201. Therefore, if you are writing
  202. programs that use random files that
  203. are not fixed (i.e., in length or
  204. the number of records) and that may
  205. be compiled and/or may run under DOS
  206. 1.1 or DOS 2.0, the "dummy
  207. end-of-file technique" will allow
  208. you to make your application
  209. transportable.
  210.