Level 2, lesson 14

# 2.14 Reading text-files

Reading text-files is pretty straightforward. You don't even have to open a file in text-mode contrary to other languages. Just open the file and call 'REFILL' until it signals end-of-file (EOF):

         \ Example program. It reads a file line by line
         \ and prints it to the screen.

         256 string line               \ define string
         " readln.4th" line copy       \ make filename

         input open                    \ open file
         if                            \ if successfully opened
              input file               \ read from file

              begin
                   refill              \ read a line
              while                    \ while EOF not found
                   0 word              \ copy to pad
                   type                \ print it
                   cr                  \ terminate line
              repeat                   \ read next line

         else
              ." Could not open file" cr quit
         then                          \ print error message

You will find that if you run this program, it will print itself to the screen.

'REFILL' will return a non-zero value if EOF was *NOT* detected. By using the word '0=' you can invert this value. Finally, it will read Unix ASCII-files as well as DOS ASCII-files.