File input/output: Difference between revisions

Content added Content deleted
m (Update Lang example: Fix spelling of Lang)
Line 2,116: Line 2,116:
w/o create-file throw r>
w/o create-file throw r>
begin
begin
pad maxstring 2 pick read-file throw
pad 84 2 pick read-file throw
?dup while
?dup while
pad swap 3 pick write-file throw
pad swap 3 pick write-file throw
repeat
repeat
Line 2,126: Line 2,126:
s" output.txt" s" input.txt" copy-file</syntaxhighlight>
s" output.txt" s" input.txt" copy-file</syntaxhighlight>


Note the use of "2 pick" to get the input file handle and "3 pick" to get the output file handle. Local or global variables could have been used, but in this implementation simple stack manipulation was chosen. Also, only maxstring bytes are copied at a time, and the global "pad" memory area is used to hold the data. For faster copies, allocating a larger buffer could be advantageous.
Note the use of "2 pick" to get the input file handle and "3 pick" to get the output file handle. Local or global variables could have been used, but in this implementation simple stack manipulation was chosen. Also, only 84 bytes are copied at a time, as that is the maximum guaranteed size of "pad" the global memory area used to hold the data. For faster copies, allocating a larger buffer could be advantageous.


Also, abort" can be used instead of throw if desired.
Also, abort" can be used instead of throw if desired.