How to Read Binary Files Using BASICs Without BINARY Access

Article translations Article translations
Article ID: 59290 - View products that this article applies to.
This article was previously published under Q59290
Expand all | Collapse all

On This Page

SUMMARY

Files written with BINARY OPEN access can be read from other BASIC products using either RANDOM access or sequential INPUT access, making use of the INPUT$ function to read in groups of bytes. Note that ASCII byte values and integers are stored the same way in most Microsoft BASICs, but the floating-point (single and double precision) formats may differ among various BASIC versions, which can complicate sharing of files created with BINARY or RANDOM access.

Note that "binary file" is a general term for a file containing a stream of bytes with no delimiters. A binary file can be written (or read) with an OPEN access of BINARY or RANDOM in BASIC. To make sense of a binary file, you must also know the record definition (or byte boundaries) of all types of variables stored in the file.

To illustrate the above concepts, examples are provided below. Example 2a or 2b running in Microsoft QuickBASIC (b) (IEEE binary math version) 1.00 for the Apple Macintosh can read IEEE floating-point numbers stored in a file that was created with BINARY or RANDOM access in QuickBASIC for MS-DOS (in Example 1a or 1b) and uploaded to the Macintosh. These products share the same IEEE format, even though they run on different computers. (Note: To transfer the file, you need special serial communications or uploading software/hardware capable of transferring binary data files between MS-DOS and Macintosh computers.)

MORE INFORMATION

Do not confuse the terms "binary file" and OPEN FOR BINARY, which are different concepts. A binary file is a type of file, while FOR BINARY is an access mode of the OPEN statement (for opening ANY type of file).

The BINARY access mode of the OPEN statement is supported in Microsoft QuickBASIC Versions 4.00, 4.00b, and 4.50 for MS-DOS, Microsoft BASIC Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2, and Microsoft BASIC Professional Development System (PDS) Version 7.00 for MS-DOS and MS OS/2. (These product versions also support a new third variable argument on the PUT# and GET# statements to more conveniently write variables to BINARY or RANDOM access files, without the need for a FIELD statement).

However, the BINARY OPEN access mode (and the third variable argument on the PUT# and GET# statements) are NOT features in the following products: in Microsoft QuickBASIC Versions 1.00, 1.01, 1.02, 2.00, 2.01, and 3.00 for MS-DOS; in Microsoft BASIC Compiler Versions 5.35 and 5.36 for MS-DOS; in Microsoft GW-BASIC Versions 3.20, 3.22, 3.23, and earlier; and in Microsoft QuickBASIC Version 1.00 for the Apple Macintosh.

For QuickBASIC (b) for Macintosh, the numeric conversion from a MS-DOS QuickBASIC 4.00, 4.00b, or 4.50 BINARY or RANDOM file is simple with the CVI, CVL, CVS, and CVD functions. QuickBASIC (d) (decimal math version) for Macintosh can use CVI and CVL, but not CVS or CVD, for this conversion.

Since MS-DOS BASICs earlier than QuickBASIC 4.00, 4.00b, or 4.50 do not support the LONG type and their floating-point format is not IEEE (except for QB87.EXE in QuickBASIC 3.00), the numbers stored as strings in their BINARY or RANDOM files are unreadable by CVS and CVD in QuickBASIC for the Macintosh. Integers are readable with the CVI function. To convert other numeric values (LONG integer, single precision, and double precision), you would have to do complicated bit manipulation based on the IEEE format to parse the string into a number recognizable in that BASIC version.

For more information concerning the IEEE format, please see Pages 131-134 in the "Microsoft Macro Assembler 5.1: Programmer's Guide," or the "IEEE and Rounding" application note, which can be obtained by calling Microsoft Technical Support at (206) 454-2030. More information can also be found in this database by querying on the following words:
floating and point and format and IEEE
Examples 1a and 1b below create an identical BINARY.DAT binary file on disk for a given BASIC.

Example 1a

The following code outputs a file with strings containing numeric values. Run this in QuickBASIC 4.00, 4.00b, or 4.50 for MS-DOS, BASIC compiler 6.00 or 6.00b for MS-DOS, or in BASIC PDS 7.00 for MS-DOS (which all support the OPEN FOR BINARY mode and the third PUT# variable argument). Examples 1a and 1b create an identical BINARY.DAT file on disk for a given BASIC.
   OPEN "binary.dat" FOR BINARY AS #1
   i% = 1
   l& = 2
   s! = 3.45!
   d# = 6.789#
   PUT #1, , i%
   PUT #1, , l&
   PUT #1, , s!
   PUT #1, , d#
   CLOSE #1
				

Example 1b

The following code outputs a file with strings containing numeric values. Run this in QuickBASIC 4.00, 4.00b, or 4.50 for MS-DOS, BASIC compiler 6.00 or 6.00b for MS-DOS, in BASIC PDS 7.00 for MS-DOS, or even in QuickBASIC (b) for Macintosh. (Examples 1a and 1b create an identical BINARY.DAT file on disk for a given BASIC.) Example 1b uses the FIELD statement technique (with RANDOM access mode):
   OPEN "binary.dat" AS #1 LEN = 18
   FIELD #1, 2 AS i$, 4 AS lo$, 4 AS S$, 8 AS D$
   i% = 1
   lo& = 2
   s! = 3.45!
   d# = 6.789#
   LSET i$ = MKI$(i%)
   LSET lo$ = MKL$(lo&)
   LSET s$ = MKS$(s!)
   LSET d$ = MKD$(d#)
   PUT #1,1
   CLOSE #1
				

Example 2a

The following program, when run in QuickBASIC (b) for Macintosh (or run in any other BASIC listed above that uses IEEE format and supports the LONG integer data type), will read the BINARY.DAT file created by Example 1a or 1b. This program opens the file FOR INPUT (sequential access) and invokes the INPUT$ function:
   OPEN "binary.dat" FOR INPUT AS #1
   i$ = INPUT$(2, 1)    ' Inputs 2 bytes
   lo$ = INPUT$(4, 1)   ' Inputs 4 bytes
   s$ = INPUT$(4, 1)    ' Inputs 4 bytes
   d$ = INPUT$(8, 1)    ' Inputs 8 bytes
   PRINT CVI(i$), CVL(lo$), CVS(s$), CVD(d$)
   CLOSE #1
				

Example 2b

The following program, when run in QuickBASIC (b) for the Macintosh (or run in any other BASIC listed above that uses IEEE format and supports the LONG integer data type), will also read the file created by and uploaded from Example 1a or 1b. Example 2b uses the FIELD statement technique (with RANDOM access mode):
   OPEN "binary.dat" AS #1 LEN = 18
   FIELD #1, 2 AS i$, 4 AS lo$, 4 AS S$, 8 AS D$
   GET#1,1
   PRINT CVI(i$), CVL(lo$), CVS(s$), CVD(d$)
   CLOSE #1
				

Properties

Article ID: 59290 - Last Review: January 9, 2003 - Revision: 1.1
APPLIES TO
  • Microsoft QuickBasic Compiler for Macintosh 1.0
Keywords: 
KB59290
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com