# 2.17 Parsing a textfile

As we've already seen, it is very easy to enter a line using 'REFILL' and parse it. You can also use 'REFILL' to read lines from a text-file. It is quite similar to reading lines from the keyboard, except that you have to open a file first. This little program prints all the words of a textfile on a new line:

         " file.txt" pad copy               \ create filename
         input open                    \ open the file

         if
           input file                  \ redirect to file
         else                          \ else error
           ." File could not be opened" cr quit
         then

         begin
              refill                   \ get a line from file
         while                         \ check if EOF
              begin
                   bl word count       \ if not, parse line
                   dup 0<>             \ check if zero length
              while
                   type cr             \ if not, print word
              repeat                   \ parse next word
              drop drop                \ drop address/count
         repeat                        \ get next line

Now that flag left by 'REFILL' makes sense! If it is zero, we have reached the end of the line. Note that you don't have to open a file in text-mode (there are even no words in 4tH to do that) and both Microsoft ASCII and Unix ASCII files are supported.