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