Reading a Text File

The following shows an example of reading a file. Program COUNT.CMD counts the words in a text file. To run it, enter Rexx COUNT followed by the name of the file to be processed:

rexx count myfile.txt
rexx count r:\rexx\articles\devcon7.scr

COUNT uses the String method WORDS to count the words, so COUNT actually counts whitespace-delimited tokens:

/* COUNT.CMD - counts the words in a file                          */
parse arg path              /* Get file name from command line     */
count=0                     /* Initialize a counter                */
file=.stream~new(path)      /* Create a stream object for the file */
do while file~lines<>0      /* Loop as long as there are lines     */
  text=file~linein          /* Read a line from the file           */
  count=count+(text~words)  /* Count words and add to counter      */
end
say count                   /* Display the count                   */

To read a file, COUNT first creates a Stream object for the file by sending the NEW message to the Stream class. The file name (with or without a path) is specified as an argument on the NEW method.

Within the DO loop, COUNT reads the lines of the file by sending LINEIN messages to the stream object (pointed to by the variable File). The first LINEIN message causes Rexx to open the file (the NEW method does not open the file). LINEIN, by default, reads one line from the file, starting at the current read position.

Rexx returns only the text of the line to your program, but no new-line characters.

The DO loop is controlled by the expression "file~lines<>0". The LINES method returns the number of lines remaining to be read in the file, so Rexx processes the loop until no lines remain to be read.

In the COUNT program, the LINEIN request forces Rexx to open the file, but you can also open the file yourself using the OPEN method of the Stream class. By using the OPEN method, you control the mode in which Rexx opens the file. When Rexx implicitly opens a file because of a LINEIN request, it tries to open the file for both reading and writing. If that fails, it opens the file for reading. To ensure that the file is opened only for reading, you can modify COUNT as follows:

/* COUNT.CMD - counts the words in a file                          */
parse arg path              /* Get file name from command line     */
count=0                     /* Initialize a counter                */
file=.stream~new(path)      /* Create a stream object for the file */
openrc=file~open("read")    /* Open the file for reading           */
if openrc<>"READY:" then do /* Check the return code               */
  say "Could not open" path||"~ RC="||openrc
  exit openrc               /* Bail out                            */
end
do while file~lines<>0      /* Loop as long as there are lines     */
  text=file~linein          /* Read a line from the file           */
  count=count+(text~words)  /* Count words and add to counter      */
end
file~close                  /* Close the file                      */
say count                   /* Display the count                   */

The CLOSE method, used near the end of the previous example, closes the file. A CLOSE is not required. Rexx closes the stream for you when the program ends. However, it is a good idea to CLOSE streams to make the resource available for other uses.