Microsoft KB Archive/38278
Article ID: 38278
Article Last Modified on 11/21/2006
This article was previously published under Q38278
When a numeric data type (INTEGER, LONG, SINGLE, DOUBLE, or CURRENCY) stored in a user-defined TYPE is PUT directly to a random-access file as the third argument of the PUT statement, the values stored in the file are stored the same as those written with PUT using variables defined in a FIELD statement and initialized with a statement such as LSET A$ = MKI$(i).
This is true for all TYPEs, except if you compile with the /MBF option. When you compile with /MBF, SINGLE or DOUBLE user-defined TYPEs still store on disk in IEEE format instead of in MBF format, which is not what you want when you compile /MBF.
This information applies to QuickBasic versions 4.0, 4.0b, and 4.5; Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS and MS OS/2; and Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2.
When you compile with the /MBF compiler option, you cannot use a SINGLE or DOUBLE declaration in user-defined variables that are used as the third argument of a random-access PUT. When compiling /MBF, you must use STRING*4 and STRING*8 to PUT single- and double-precision numbers that are in a user-defined TYPE; this requires using MKS$, MKD$, CVS, and CVD for conversion of those numbers between string and numeric TYPEs.
More information on this subject can be found under "Using Random-Access Files" on pages 126-134 in the "Microsoft QuickBasic 4.0: Programming in Basic: Selected Topics" manual supplied with QuickBasic version 4.0 or 4.0b; in the "Microsoft Basic Compiler 6.0: Programming in Basic: Selected Topics" manual supplied with Microsoft Basic Compiler version 6.0 or 6.0b; or starting on page 102 in the "Microsoft Basic 7.0: Programmer's Guide" for Microsoft Basic PDS 7.0 and 7.1.
User-defined TYPEs are not available in earlier versions.
If you define an integer, such as in a user-defined TYPE, it is stored as 2 bytes. A long integer is stored as 4 bytes. A single-precision variable is stored as 4 bytes. A double-precision variable is stored as 8 bytes. Fixed-length strings are stored with 1 byte per character.
Variable-length-string variables (such as x$ AS STRING) cannot be placed in a user-defined TYPE record. For information about the special way variable-length strings are stored on disk when written directly as the third argument of the PUT# statement, query on the following words:
PUT and THIRD and "BAD RECORD LENGTH" and VARIABLE and STRING
The code example below shows two equivalent methods of reading and writing to random files when you compile without the /MBF option. One method uses user-defined-TYPE records, and the other (older) method uses variable-length strings in a FIELD statement to define the file record. Note how much simpler your code is when you use user-defined TYPE records instead of records defined by a FIELD statement.
' Note: This program is NOT compatible with the /MBF compiler ' option because of the single- and double-precision TYPEs in the ' user-defined TYPE. When compiling /MBF, you must use STRING*4 ' and STRING*8 to PUT single- and double-precision numbers that ' are in a user-defined TYPE, which requires rewriting this program ' to use MKS$, MKD$, CVS, and CVD for conversion of those numbers. ' INTEGER, LONG, and STRING TYPEs are independent of the /MBF option. TYPE Users i AS INTEGER l AS LONG s AS SINGLE d AS DOUBLE a AS STRING * 15 END TYPE DIM dat AS Users dat.i = 32000 dat.l = 21345678 dat.s = 1234.567 dat.d = 98765.54321# dat.a = "first data set" CLS ' The following outputs a user-defined-TYPE record to the file all ' at once: OPEN "test.dat" FOR RANDOM AS #1 LEN = LEN(dat) PUT #1, 1, dat CLOSE ' The following inputs from the file into FIELDed variables: OPEN "test.dat" FOR RANDOM AS #1 LEN = 33 FIELD #1, 2 AS a$, 4 AS B$, 4 AS C$, 8 AS d$, 15 AS e$ GET #1, 1 i1% = CVI(a$) l1& = CVL(B$) s1! = CVS(C$) d1# = CVD(d$) PRINT i1%, l1&, s1!, d1#, e$ ' The following outputs a new record using the FIELDed variables: LSET e$ = "New data " PUT #1, 2 CLOSE ' The following inputs from the file into a user-defined variable: OPEN "test.dat" FOR RANDOM AS #1 LEN = 33 GET #1, 2, dat PRINT dat.i, dat.l, dat.s, dat.d, dat.a CLOSE
Additional query words: QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10