Article ID: 59290 - View products that this article applies to.
This article was previously published under Q59290
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.)
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 IEEEExamples 1a and 1b below create an identical BINARY.DAT binary file on disk for a given BASIC.
Example 1aThe 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.
Example 1bThe 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):
Example 2aThe 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:
Example 2bThe 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):
Article ID: 59290 - Last Review: January 9, 2003 - Revision: 1.1