Product SiteDocumentation Site

14.4. Examples of Input and Output

In most circumstances, communication with a user running a Rexx program uses the default input and output stream objects. For a question and answer dialog, the recommended technique is to use the SAY and PULL instructions on the .INPUT and .OUTPUT objects. (You can use PARSE PULL if case-sensitive input is needed.)
It is generally necessary to write to, or read from, stream objects other than the default. For example, the following program copies the contents of one stream to another.

Example 14.4. FILECOPY program

/* FILECOPY.CMD */
/* This routine copies, as lines, the stream or     */
/* file that the first argument names to the stream */
/* or file the second argument names. It is assumed */
/* that the name is not an object, as it could be   */
/* if it is passed from another Rexx program.       */

parse arg inputname, outputname

inputobject = .stream~new(inputname)
outputobject = .stream~new(outputname)

signal on notready

do forever
  outputobject~lineout(inputobject~linein)
end
exit

notready:
return

As long as lines remain in the named input stream, a line is read and is then immediately written to the named output stream. This program is easy to change so that it filters the lines before writing them.
The following example illustrates how character and line operations can be mixed in a communications program. It converts a character stream into lines.

Example 14.5. COLLECT program

/* COLLECT.CMD */
/* This routine collects characters from the stream */
/* the first argument names until a line is         */
/* complete, and then places the line on the        */
/* external data queue.                             */
/* The second argument is a single character that   */
/* identifies the end of a line.                    */
parse arg inputname, lineendchar
inputobject = .stream~new(inputname)

buffer=""      /* zero-length character accumulator */
do forever
  nextchar=inputobject~charin
  if nextchar=lineendchar then leave
  buffer=buffer||nextchar          /* add to buffer */
  end
queue buffer /* place it on the external data queue */

Here each line is built up in a variable called BUFFER. When the line is complete (for example, when the user presses the Enter key) the loop ends and the language processor places the contents of BUFFER on the external data queue. The program then ends.