BINARY

From QB64 Wiki
Redirect page
Jump to navigation Jump to search

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

  • OPEN FOR BINARY creates the file if the fileName$ does not exist.
  • fileName$ is the name of the file to open, but could also configure a port.
  • #fileNumber% is the number that will represent the file when performing file operations.
  • BINARY files use GET # and PUT # to read or write the file at any byte position.
  • In version 1.000 and up you can use LINE INPUT in BINARY mode for faster access to text file data.
  • QBasic's BSAVE or BINARY save files can be read using BINARY mode.
  • BINARY mode ignores any LEN = recordlength statement in the OPEN statement.
  • Ports can also be opened in BINARY mode. See: OPEN COM


Examples

Example 1: 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.


Example 2: 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



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page