fileiolib
A file is a Blitz object, numbered from 0. OpenFile, ReadFile and
WriteFile open one and return true (-1) or false (0), so a program can
test whether it worked. Every file goes through a buffer of its own,
which makes reading and writing fast, especially when it goes on from
one byte to the next; DosBuffLen sets its size. Open files are closed
when the program ends, but a program should close them with CloseFile
when it no longer needs them.
A file can be used in two ways. Sequentially, FileOutput sends the
output of Print and NPrint into the file, and FileInput makes
Edit$, Edit and Inkey$ read from it, line by line. As records,
Fields names the variables of a record, and Put and Get write or
read record number n of the file:
MaxLen name$=20
If OpenFile(0,"RAM:people")
Fields 0,name$,age
name$="Ada" : age=36
Put 0,0
CloseFile 0
EndIfReadMem and WriteMem move whole blocks of memory, FileSeek and
Loc set and tell the position, and Lof and Eof tell the length and
whether the end is reached.
Based on the fileiolib guide by Acid Software.
Library number 124. It manages File objects, by default at most 5 (the compiler options set the maximum).
Commands
CloseFile
Writes what is left in the buffer, closes the file and frees its buffer and its record fields.
Statement. Help text: File#
Arguments: .w
DosBuffLen
The size in bytes of the buffer of files opened after it; the start value is 2048. A smaller buffer saves memory.
Statement. Help text: Bytes
Arguments: .l
With runtime error checking, a size above 32767 is the error “Illegal Dos Buffer Length”.
Eof
True (-1) if the position is at the end of the file, else false (0). At the end, writing makes the file longer.
Function, result .l. Help text: (File#)
Arguments: .w
Exists
The size in bytes of the file, or 0 if it does not exist or cannot be examined. A drawer or the root of a volume gives -1.
Function, result .l. Help text: (Filename$)
Arguments: $
An empty file also gives 0, so Exists cannot tell it from a missing
file; ReadFile can. This is kept as in the original
(#143), because
programs use the result as the length of the file.
Fields
Names the variables that make up a record of the file, for Put and Get. A string variable must have a maximum length set with MaxLen and must never grow beyond it.
Statement. Help text: File#,Var1[,Var2...]
Arguments: .w, { &any }...
The fields are stored in the file in the reverse order of the list,
the last variable first. A second Fields adds to the fields already
set, so a record can be named in several statements; CloseFile
clears them.
File
The object of an open file, as the introduction describes.
Object name. No help text.
FileInput
Makes the following Edit$, Edit and Inkey$ read from the file, a line at a time for Edit$ and Edit. Before the file is closed, switch the input back, for example with DefaultInput or WindowInput.
Statement. Help text: File#
Arguments: .w
FileOutput
Sends the output of the following Print and NPrint commands into the file. Before the file is closed, switch the output back, for example with DefaultOutput or WindowOutput.
Statement. Help text: File#
Arguments: .w
FileSeek
Moves to a position in the file; the first byte is position 0. The position may be at most the length of the file, so FileSeek 0,Lof(0) goes to the end, to add to the file. A position that cannot be reached is the error “Can’t FileSeek Position”.
Statement. Help text: File#,Position
Arguments: .w, .l
Get
Reads record number Record (counted from 0) of the file into the Fields variables. A record beyond the end of the file is the error “Can’t Find Record”.
Statement. Help text: File#,Record
Arguments: .w, .w
KillFile
Deletes a file. Nothing tells whether that worked.
Statement. Help text: Filename$
Arguments: $
Loc
The current position in the file; it is 0 after opening.
Function, result .l. Help text: (File#)
Arguments: .w
Lof
The length of the file in bytes, including what is still in the buffer.
Function, result .l. Help text: (File#)
Arguments: .w
OpenFile
Opens a file for reading and writing and returns true (-1), or false (0) if it cannot be opened. A file that does not exist is created. A file the object held before is closed first.
Function, result .w. Help text: (File#,Filename$)
Arguments: .w, $
Meant for records with Fields, Put and Get, but sequential use
works too.
Put
Writes the values of the Fields variables as record number Record (counted from 0) of the file. A new record may only be added right at the end of the file; a record further on is the error “Can’t Find Record”.
Statement. Help text: File#,Record
Arguments: .w, .w
ReadFile
Opens an existing file for reading and returns true (-1), or false (0) if it cannot be opened; FileInput then reads from it. A file the object held before is closed first.
Function, result .w. Help text: (File#,Filename$)
Arguments: .w, $
ReadMem
Reads Length bytes from the file into memory at Address. Make sure that memory belongs to the program.
Statement. Help text: File#,Address,Length
Arguments: .w, .l, .l
WriteFile
Creates a new file, replacing one of the same name, and returns true (-1), or false (0) if it cannot be created; FileOutput then writes to it. A file the object held before is closed first.
Function, result .w. Help text: (File#,Filename$)
Arguments: .w, $
WriteMem
Writes Length bytes from memory at Address into the file.
Statement. Help text: File#,Address,Length
Arguments: .w, .l, .l