Reading a Text File

The following shows an example of reading a file. Program count&per;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&per;cmd uses the String method WORDS to count the words, so count&per;cmd actually counts whitespace-delimited tokens:


count&per;cmd with automatic open
/* count&per;cmd - counts the words in a file */
parse arg filename /* Get file name from command line */
count=0 /* Initialize a counter */
file=.stream~new(filename) /* Create a stream object for the file */
do while file~lines&gt.&lt.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&per;cmd 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&per;cmd 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 automatically open the file if it was not already open (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 without any line-end 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&per;cmd 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 tries to open the file for reading. To ensure that the file is opened only for reading, you can modify count&per;cmd as follows:


count&per;cmd with explicit open
/* count&per;cmd - counts the words in a file */
parse arg filename /* Get file name from command line */
count=0 /* Initialize a counter */
file=.stream~new(filename) /* Create a stream object for the file */
openrc=file~open("read") /* Open the file for reading */
if openrc&gt.&lt."READY:" then do /* Check the return code */
say "Could not open" path¦¦"~ RC="¦¦openrc
exit openrc /* Bail out */
end
do while file~lines&gt.&lt.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.