open -- open a file
open a, "File", "r+"
open "file" for reading as a
open "file" for writing as #a
if (open(a,"file")) ...
if (open(a,"file","w")) ...
The open-command opens a file for reading or writing for printing text. open comes in a wide variety of ways; it requires these arguments:filenumber In the synopsis this is a or #a. In yab each file is associated with a number between 1 and a maximum value, which depends on the operating system.
For historical reasons the filenumber can be preceded by a hash ('#'). Note, that specifying a filenumber is optional; if it is omitted, the open-function will return a filenumber, which should then be stored in a variable for later reference. This filenumber can be a simple number or an arbitrary complex arithmetic expression, in which case braces might be necessary to save yab from getting confused.
filename -- In the synopsis above this is "file". This string specifies the name of the file to open (note the important caveat on specifying these filenames).
accessmode -- In the synopsis this is "r", "w", for reading or for writing. This string or clause specifies the mode in which the file is opened; it may be one of: "r" Open the file for reading (may also be written as for reading). If the file does not exist, the command will fail. This mode is the default, i.e. if no mode is specified with the open-command, the file will be opened with this mode.
"r" -- Open the file for reading (may also be written as for reading). If the file does not exist, the command will fail.
"r+" -- Open the file for reading and writing. If the file does not exist, it will be created. Writing is limited to poke, print #a will not work in this mode.
"w" -- Open the file for writing (may also be written as for writing). If the file does not exist, it will be created.
"a" -- Open the file for appending, i.e. what you write to the file will be appended after its initial contents. If the file does not exist, it will be created.
"b" -- This letter may not appear alone, but may be combined with the other letters (e.g. "rb") to open a file in binary mode (as opposed to text mode).
As you may see from the synopsis, the open-command may either be called as a command (without braces) or as a function (with braces). If called as a function, it will return the filenumber or zero if the operation fails. Therefore the open-function may be used within the condition of an if-statement.
If the open-command fails, you may use peek("error") to retrieve the exact nature of the error.
Finally you may read the description for peek("error") to learn which errors may have happened during an open-call.
open "foo.bar" for writing as #1
print #1 "Hallo!"
if (not open(1,"foo.bar")) error "Could not open 'foo.bar' for reading"
line input #1 a$
This example simply opens the file foo.bar, writes a single line, reopens it and reads its contents again.
close, print, peek