BINARY - QB64 Wiki

BINARY

From QB64 Wiki

Jump to: navigation, search

BINARY is used in an OPEN statement to treat the file or port device as a BINARY.


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.
  • QB64-GL allows LINE INPUT to be used for faster access to text file data.
  • The number of bytes read or written depends on the byte length of the variable type used.
  • When PUT is used with a variable, numerical values are converted to ASCII string types MKI$, MKL$, MKS$, MKD$ or _MK$.
  • One byte ASCII STRING data character values can be read as number values using ASC(values from 0 to 255).
  • Some files, like bitmaps, contain standard length file information headers.
  • Qbasic's BSAVE or BINARY save files can be read using a BINARY mode.
  • BINARY mode ignores any LEN = recordlength statement in the OPEN statement.
  • Ports can also be opened in BINARY mode. See: OPEN COM


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:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Views
Personal tools