How to Use MKS$, CVS with Binary Numerics in a Sequential File (71112)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 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) <> "END" ' 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) = "END" THEN EXIT DO
LOOP
OPEN "ACCOUNT.INF" FOR OUTPUT AS #1
PRINT #1, z$;
CLOSE #1

OPEN "ACCOUNT.INF" 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 "Bob Hartzell", 300.001, 400000.01, 35000, 100
DATA "Alice Provan", 150.0012, 400000.021, 35001, 101
DATA "Alex Landow", 75.00123, 400000.0311, 35002, 102
DATA "Walt Riley", 50.00123, 400000.04111, 35003, 103
DATA "Georgette Gump", 25.00123, 400000.051111, 35004, 104
DATA "END", 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$


Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB71112