QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

See OPEN.

BINARY is used in an OPEN statement to work with the file or port device manipulating its bytes directly.

Syntax

OPEN fileName$ FOR BINARY AS #fileNumber%

Description

Example(s)

Shows how a PUT variable value is converted to an ASCII string _MK$ format in a BINARY file.


DIM int64 AS _INTEGER64
DIM byte8 AS STRING * 8
int64 = 12345678
PRINT int64

OPEN "temp64.tmp" FOR BINARY AS #1
PUT #1, , int64                 'the file size will be 8 bytes
CLOSE

PRINT "Press a key to read the file!"
K$ = INPUT$(1)

OPEN "temp64.tmp" FOR BINARY AS #1
GET #1, , byte8                'GET the value as a string
PRINT "text string: "; byte8   'show that string is in _MK$ format

PRINT _CV(_INTEGER64, byte8)   'convert to numerical value
CLOSE

Note: The numerical value does not need to be converted if the file is read using the same numerical variable type as written.

A binary file viewer that can view integer values. The type of value can be changed at DIM.


SCREEN _NEWIMAGE(1000, 600, 256)
_SCREENMOVE _MIDDLE
DIM value AS INTEGER    'value type can be changed
LINE INPUT ; "Enter a BINARY filename to open: ", file$
PRINT " Press S to restart!"

IF LEN(file$) THEN OPEN file$ FOR BINARY AS #1 ELSE END
IF LOF(1) = 0 THEN PRINT "Empty file!": END
DO
  FOR i = 1 TO 16
    x = x + 1
    GET #1, , value
    IF EOF(1) THEN EXIT DO
    PRINT value;
  NEXT
  PRINT CHR$(27); x; "@"; row
  K$ = INPUT$(1)
  IF UCASE$(K$) = "S" THEN CLS: x = 0: row = 0: PRINT "Restarted!": SEEK 1, 1
  IF x = 256 THEN x = 0: row = row + 1: PRINT
LOOP UNTIL K$ = CHR$(27)
CLOSE #1
PRINT "Press Escape to exit!"
DO: _LIMIT 100
LOOP UNTIL INKEY$ = CHR$(27)
SYSTEM

See Also