2.16 Writing binary files

Writing binary files is very easy. Of course you need a buffer, like we discussed in the previous section. The program is not much different than the previous one:

     1024 constant bufsize           \ actual buffersize
     bufsize string buffer           \ define buffer

     buffer bufsize char H fill      \ fill the buffer

     64 string filename              \ define string
     " infile.dat" filename copy     \ make filename

     output open
     if                              \ open output file
           output file               \ redirect input
           buffer bufsize type       \ write to file
     else                            \ issue error message
           ." File could not be opened" cr quit

This will write 1024 "H"s to "infile.dat". The actual command that does all writing is 'TYPE'. The word 'TYPE' does not return anything. You can be assured that everything was alright, since if it wasn't, 4tH would have caught the error itself.