Original Author Paul Laughton, 2011
Page 112
De Re BASIC!
Byte File I/O
Byte file I/O can be used to read and write any type of file (.txt, .jpg, .pdf, .mp3, etc.). Each command
reads or writes one byte or a sequence of bytes as binary data.
Byte.open {r|w|a}, <file_table_nvar>, <path_sexp>
The file specified by the path string expression <path_sexp> is opened. If the path is a URL starting with
"http..." then an Internet file is opened. Otherwise, the <path_sexp> string is appended to the default
path "<pref base drive>/rfo-basic/data/".
The first parameter is a single character that sets the I/O mode for this file:
Parameter
Mode
Notes
r
read
File exists: Reads from the start of the file.
File does not exist: Error (see below).
w
write
File exists: Writes from the start of the file. Writes over any existing data.
File does not exist: Creates a new file. Writes from the start of the file.
a
append
File exists: Writing starts after the last line in the file.
File does not exist: Creates a new file. Writes from the start of the file.
A file table number is placed into the numeric variable <file_table_nvar>. This value is for use in
subsequent Byte.read.*, Byte.write.*, Byte.eof, Byte.position.*, Byte.truncate, Byte.copy, or
Byte.close commands.
If a file being opened for read does not exist then the <file_table_nvar> will be set to -1. The BASIC!
program can check for this and either create the file or report the error to the user. Information about
the error is available from the GETERROR$() function.
Byte.close <file_table_nexp>
Closes the previously opened file.
Byte.read.byte <file_table_nexp> {,<nvar>}...
Reads bytes from a file. The <file_table_nexp> parameter is a file table number returned by a previous
Byte.open. If the file number is -1 then the command throws a run-time error.
Places the byte(s) into the <nvar> parameter(s) as positive values, 0 <= byte <= 255. After the last byte in
the file has been read, further attempts to read from the file return the value -1. The result of the
Byte.eof command will be false after reading real data and true after reading at end-of-file (EOF).
This example reads a file and prints each byte, and prints "-1" at the end to show that the entire file has
been read.
BYTE.OPEN r, file_number, "testfile.jpg"
DO
BYTE.READ.BYTE file_number, Byte