File Type Binary

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC


With files opened for Binary access, data is written to the file or read from the file byte by byte as characters. See Chr$(n). Binary access files must be OPENed to be used and they must be CLOSEd when finished.


Any file that you need access to at a per byte basis can be opened as binary, but you usually open non-text files in this mode.
Data can be read or written beginning at any location in a Binary access file. A file pointer is used with Binary access files to locate a point in the file at which to read or write. The SEEK command sets the pointer to the desired spot in the file for reading or writing. The LOC(#handle) function will retrieve the current position of the pointer.
Data can be read with the INPUT$() function which reads one or more characters as specified in the function parameter.
Data is written to the file using the PRINT statement. The End-Of-Line characters, carriage return - chr$(13) - and line feed - chr$(10), are never automatically appended to data written to the file. (again: if you PRINT to a binary file, new line will NOT be inserted, contrary to sequential file).
For this reason, binary files are not good candidates for saving text files since without end-of-line characters, all sentences will run together. To create line breaks, one must add the end-of-line characters manually.


Using Binary Access Files To OPEN a file

  1. open fileName$ for binary as #handle

To CLOSE a file

  1. close #handle

To SEEK to the file position

  1. seek #handle, fpos

To LOCATE the current file position

  1. fpos = loc(#handle)

To WRITE a byte to the file

  1. #handle chr$(143) ' (Preferred form)
  2. print #handle, chr$(143) ' (Obsolete form)

To READ the data at the current location

  1. var$ = input$( #handle, sizeToRead)


LOF() and EOF() can be used with Binary access files to get the length of the file and to read to the end of the file.
When working with binary files, there is no command to truncate the file at a certain size. If the file will grow, it will stay that long until you delete it. And even if you do not use information after some point in a file, that information will still be there until you delete the file.
You cannot open files for binary that are read-only. For instance, files located on a CD-ROM.
For comparative purposes, note that files opened for Sequential access are read up to a comma delimiter or as a full line, and reading always starts at the beginning of the file. Random files are normally read as records, one full record at a time, and from any location in the file. Binary access files are read as a series of characters at a time, from any location in the file.
DO NOT use
# input #handle, txt$
on binary files - it will read file as sequential, up to a comma delimiter or new line, that doesn't make sense for binary files.
You usually don't open text files for binary, since it is easier to handle them sequentially. In fact, most binary files are program executables or image files, such as bitmaps and GIFs, and other file types in which the data is interpreted differently than ordinary textual characters. This example shows how the loc() and seek commands work, using text data that is more readable for humans.

Below find a table of the relation between file content type and preferred access mode.

File Access Modes Best Practice
Content Mime Type Typical Extension Access Mode Reading Writing Open Mode
Plain Text text/plain ini, txt, log Sequential INPUT PRINT read or write
Random Access Random GET PUT read and write (*)
Anything else bmp, gif, doc, etc. Binary INPUT$() PRINT read and write (*)

(*) The file must be WRITE-able. So attempting to open a read-only file (or file from read-only media, like CD) for RANDOM or BINARY will fail.


'binary file example

'truncate file (make it empty) first
'KILL would error if file not exist, but this will make sure file is empty
open "myfile.bin" for output as #myfile
close #myfile

txt$ = "I like programming with Just BASIC."
open "myfile.bin" for binary as #myfile

'write some data to the file
print #myfile, txt$
print "Original data in file is: ";txt$

'retrieve the position of the file pointer (since we just wrote it, it is file length)
nowPos = LOC(#myfile)

'move the file-pointer
nowPos = nowPos - len("Just BASIC.")    'move it a few words back
seek #myfile, nowPos

'read the data at the current location
txt$=input$(#myfile, nBytes)

'print txt$ in mainwin
print nBytes; " bytes of data at ";nowPos;" is: ";txt$

'move the file-pointer
seek #myfile, 2

'get current file pointer position
OldLOC = loc(#myfile)

'write some data to the middle of the file
print #myfile, "love"

'get new file pointer position
NewLOC = loc(#myfile)

'display the number of bytes written
print NewLOC - OldLOC; " bytes written at ";OldLOC

'move the file pointer to the beginning
seek #myfile, 0

'read the data - whole file
txt$ = input$( #myfile, lof( #myfile))

'print data in mainwin
print "New Data is: ";txt$

'close the file
close #myfile

Useful Procedures

' Place a useful function using this keyword here