9.24 Appending to existing files. 4tH 4tH Forth compiler
Pubblicità
Pubblicità
CHAPTER 9. CHARACTER SEGMENT
9.24
Appending to existing files
113
You can use a so-called modifier to signal 4tH, it shouldn’t overwrite the file it opens, but append to it: file OutFile \ value for output file s" outfile.dat" output open error?
abort" Cannot open file" \ open the file to OutFile \ save handle
OutFile use
10 0 do i . loop
OutFile close
\ now write to disk
\ write 0 to 9
\ close the file s" outfile.dat" output append + open error?
abort" Cannot open file" to OutFile
OutFile use
20 10 do i . loop
OutFile close
\ reopen in append mode
\ save handle
\ now write to disk
\ write 10 to 19
\ close the file
Take a look at the contents of this file after you’ve run the program and you’ll find it contains the number 0 to 19.
9.25
Using pipes
If you’re using Windows 95 OSR2 (and up), Windows NT (and up), OS/X, Linux or another
Unix system you’re in for a treat! With Unix you can do neat tricks like this: ls | mail root
Which means you can redirect the output of ’ls’ to ’mail’, so in effect you send an email to root with the contents of your current working directory. Yes, 4tH can do this too, but you can do even more. You can start ’ls’ and read its output line by line as if it were a file. You can also start ’mail’ and write the output of a 4tH program to it. We do that by opening a pipe to a program.
If you’ve ever written a program using C, you know this is a bit cumbersome, since you’ve got to use special functions to use pipes. In 4tH you don’t. Just let 4tH know it’s a pipe that you’re opening and not a file: file InFile file OutFile
\ value for input file
\ value for output file s" ls" input pipe + open error?
s" mail hans" output pipe + open error? rot or abort" Cannot open pipe" to OutFile to InFile
The only thing you have to do to signal 4tH that you’re using a pipe is add the word ’PIPE’, just like ’APPEND’. The filename is replaced by the command you want to execute. That’s all. If one of the pipes in this program fails, the program aborts.
InFile use OutFile use
." These are the contents of my current working directory:" cr cr
CHAPTER 9. CHARACTER SEGMENT 114
Now we can treat our pipes just as if they are ordinary files. We redirect input and output and write a nice header to our email. Now we can start to process the output of ’ls’: begin refill while
0 parse-word type cr repeat
Note that we don’t have to signal that we’re reading the pipe to ’ls’ as a text file. We just read it line by line until ’REFILL’ returns zero. Then we can parse the line and ’TYPE’ it to ’mail’.
InFile close OutFile close
Of course you don’t have to close the pipe, but it won’t harm when you don’t. 4tH knows what to do. After executing this program, Hans will receive this email:
From: Hans Bezemer <[email protected]>
Message-Id: <[email protected]>
Status: RO
These are the contents of my current working directory:
4th.c
4thc.c
4thd.c
4thg.c
4thx.c
Well, that wasn’t too hard, was it?
9.26
Opening a file in read/write mode
For special purposes you might want to open a file in read/write mode. That’s quite easy: file InOutFile \ value for output file s" outfile.dat" output input + open error?
abort" Cannot open file" \ open the file to InOutFile \ save handle
Just add both together like you adding a modifier. Note that once you ’USE’ this file, you’re both reading and writing to this file. Furthermore, the file has to exist otherwise you get an error. If you want to write to a new file, you first have to open it in write mode: file InOutFile \ value for output file
\ create a new file s" outfile.dat" output open close s" outfile.dat" output input + open error?
abort" Cannot open file" \ open the file to InOutFile \ save handle
Scarica
Pubblicità