Liberty BASIC 3 provides a binary access mode. Binary mode means manipulating the bytes in a file. The bits and bytes must be manipulated and written to the file or read from the file as characters. There are 8 bits in a byte, and a byte is written to the file as a character... chr$(n). Use the SEEK command to seek to the desired point in the file for reading or writing. Use the LOC(#handle) function to get the current position. This mode never writes line delimiters when printing to the file.
Sequential files must be written in their entirety. Random files may write single records within a file, but they are limited to writing at record boundaries and they may only write an entire record. Binary mode allows us to open a file, seek to the desired location anywhere in the file, then write data of any desired length to replace the existing data at that location in the file.
Here's the way to open a file for binary access:
open "myfile.ext" for binary as #handle
Here is the way to set the file pointer to the desired position in the file:
fpos=1024 seek #handle, fpos
seek #handle, 1024
'get the current file position fpos = loc(#handle)
'write a byte to the file print #handle, chr$(143)
'mock example open "myfile.bin" for binary as #myfile seek #myfile, 1024 print #myfile, "write this into the file" notice str$(loc(#myfile) - 1024); " bytes written" close #myfile end
We can use binary file access to replace the torch icon in the runtime engine with another 16-color icon of our own choice. See the next section for binary access in action.