Next: , Previous: Booleans, Up: Language


2.6 Files

Files can only be created using the built-in function open. Files are either readable or writeable. If a file is writeable, text written to the file may optionally be appended to any existing text.

"a" "r" ! open = f1;
"b" "w" ! open = f2;
"c" "a" ! open = f3;

# Read one character
f1 ! read | stdout temp ! write;
# Write a string
f2 "Aaar" ! write;
# Append a string
f3 "Ouu" ! write;

f1!close;f2!close;f3!close;

To read an entire file, one must use a loop construct. See Loops. When there are no more characters left, the read function returns an empty string. Using equality testing, reading a complete file is thus possible. See Conditionals.

filename "r" ! open = f;
{f ! read = c; stdout c ! write; c "" ! equal ! not = return;} ! loop;

At this point, it may not be obvious why this works. This will be explained in the next sections.