Microsoft KB Archive/71112

= How to Use MKS$, CVS with Binary Numerics in a Sequential File =

Article ID: 71112

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q71112



SUMMARY
This article discusses how to use the string conversion functions (MKS$, MKD$, MKI$, MKL$, CVS, CVD, CVI, CVL) to convert binary-format numbers stored in a file opened with sequential access. (Note that string conversion functions are normally used with files opened with random access, not sequential access). You might use this technique if you prefer sequential access statements, and want the numeric data unreadable when loaded in word processors or when typed from DOS.

The example below applies to Microsoft QuickBasic versions 4.00, 4.00b, and 4.50 for MS-DOS; to Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS-DOS and MS OS/2; and to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS and MS OS/2.

The same INPUT$ technique can be used in the following earlier versions of Microsoft Basic (you need to modify the code example to account for Basic dialect differences): in QuickBasic 1.00, 1.01, 1.02, 2.00, 2.01, and 3.00 for MS-DOS; in GW-Basic versions 3.20, 3.22, and 3.23 for MS-DOS; and in QuickBasic for the Apple Macintosh.



MORE INFORMATION
The following example appends all data into one string. The numeric data is turned into string form with the MKS$, MKD$, MKL$, and MKI$ statements, and put into the file with a single PRINT# statement.

By using the INPUT$ statement, the program reads the data back into string variables. The numbers stored in the string variables are converted to numeric variables by the CVS, CVD, CVL, and CVI functions.

Code Example
DIM accname AS STRING * 15        ' Dimension variables. DIM sngl AS SINGLE DIM dbl AS DOUBLE DIM lng AS LONG DIM intgr AS INTEGER READ accname, sngl, dbl, lng, intgr ' Read variables from DATA. DO WHILE UCASE$(accname) <> &quot;END&quot; ' Check for end then append to string. z$ = accname + MKS$(sngl) + MKD$(dbl) + MKL$(lng) + MKI$(intgr) + z$ READ accname, sngl, dbl, lng, intgr  ' Read variables from DATA. IF RTRIM$(accname) = &quot;END&quot; THEN EXIT DO LOOP OPEN &quot;ACCOUNT.INF&quot; FOR OUTPUT AS #1 PRINT #1, z$; CLOSE #1

OPEN &quot;ACCOUNT.INF&quot; FOR INPUT AS #2 PRINT : PRINT

DO WHILE NOT EOF(2) nam$ = INPUT$(15, 2)  ' Input variables from file. sing$ = INPUT$(4, 2) doub$ = INPUT$(8, 2) lon$ = INPUT$(4, 2) int$ = INPUT$(2, 2) PRINT nam$; CVS(sing$), CVD(doub$), CVL(lon$), CVI(int$) LOOP END

DATA &quot;Bob Hartzell&quot;, 300.001, 400000.01, 35000, 100 DATA &quot;Alice Provan&quot;, 150.0012, 400000.021, 35001, 101 DATA &quot;Alex Landow&quot;, 75.00123, 400000.0311, 35002, 102 DATA &quot;Walt Riley&quot;, 50.00123, 400000.04111, 35003, 103 DATA &quot;Georgette Gump&quot;, 25.00123, 400000.051111, 35004, 104 DATA &quot;END&quot;, 0,0,0,0 Reference:

For another article concerning how to read a binary file opened with sequential access, search for the following words:

read and binary and random and sequential and INPUT$

Additional query words: QuickBas BasicCom

Keywords: KB71112

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.