Original Author Paul Laughton, 2011
Page 109
De Re BASIC!
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 Text.readln, Text.writeln, Text.eof, Text.position.*, or Text.close commands.
If a file being opened for read does not exist then the <file_table_nvar> will be set to -1. The BASIC!
programmer 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.
Opening a file for append that does not exist creates an empty file. Finally, opening a file for write that
already exists deletes the contents of the file; that is, it replaces the existing file with a new, empty one.
Text.close <file_table_nexp>
The previously opened file represented by <file_table_nexp> will be closed.
Note: It is essential to close an output file if you have written over 8k bytes to it. If you do not close the
file then the file will only contain the first 8k bytes.
Text.readln <file_table_nexp> {,<svar>}...
Read the lines from the specified, previously opened file and write them into the <svar> parameter(s). If
<file_table_nexp> is -1, indicating Text.open failed, or if it is not a valid file table number, then the
command throws a run-time error.
After the last line in the file has been read, further attempts to read from the file place the characters
"EOF" into the <line_svar> parameter(s). This is indistinguishable from the string "EOF" read as actual
data, except that the result of the Text.eof command will be false after reading real data and true after
reading at end-of-file (EOF).
This example reads an entire file and prints each line.
TEXT.OPEN r, file_number, "testfile.txt"
DO
TEXT.READLN file_number, line$
PRINT line$
UNTIL line$ = "EOF"
TEXT.CLOSE file_number
The file will not automatically be closed when the end-of-file is read. Subsequent reads from the file will
continue to return "EOF".