You probably don't want to write programs that only write to the screen and read from the keyboard. So 4tH has a few words that allow you to work with files. Since 4tH is a scripting language, the capabilities are limited. But you will find that you can perform most common operations.
One of the limitations is that you can have one input-file and one output-file open at the time. If you open another, the first one will be closed.
Opening a output-file is pretty simple. Just throw the address of a filename and a read/write-mode flag on the stack and execute the word 'OPEN':
64 string filename " outfile.dat" filename copy ( -- a) output open ( a -- f) 0= if ." File could not be opened" cr quit then
'OUTPUT' is a write-flag and will open a file for writing. 'OPEN' leaves a value on the stack. If it is a "true" value, the file was succesfully opened. If not, it wasn't.
4tH will allow you to open another, but automatically closes the previously opened file. However, if you open an input-file the output- file stays open. The syntax is the same, except for the read-flag 'INPUT' of course:
64 string filename " infile.dat" filename copy input open 0= if ." File could not be opened" cr quit then
A second input-file closes the first one, just like an output-file.