5.3. Short Crash-Course

REXX I/O is very simple, and this short crash course is probably
all you need in a first-time reading of this chapter. But note
that that, we need to jump a bit ahead in this section.

To read a line from a stream, use the LINEIN() built-in function,
which returns the data read. To write a stream, use the LINEOUT()
built-in function, and supply the data to be written as the second
parameter. For both operations, give the name of the stream as the
first parameter. Some small examples:

     contents = linein( ‘myfile.txt’ )
     call lineout ‘yourfile.txt’, ‘Data to be written’

The first of these reads a line from the stream myfile.txt, while
the second writes a line to the stream yourfile.txt.  Both these
calls operate on lines and they use a system specific end-of-line
marker as a delimiter between lines. The marker is tagged on at
the end of any data written out, and stripped off any data read.

Opening a stream in REXX is generally done automatically, so you
can generally ignore that in your programs.  Another useful method
is repositioning to a particular line:

     call linein ‘myfile.txt’, 12, 0
     call lineout ‘yourfile.txt’,, 13

Where the first of these sets the current read position to the
start of line 12 of the stream; the second sets the current write
position to the start of line 13. Note that the second parameter
is empty, that means no data is to be written. Also note that the
current read and write positions are two independent entities;
setting one does not affect the other.

The built-in functions CHARIN() and CHAROUT() are similar to the
ones just described, except that they are character-oriented, i.e.
the end-of-line delimiter is not treated as a special character.

Examples of use are:

     say charin( ‘myfile.txt’, 10 )
     call charout ‘logfile’, ‘some data’

Here, the first example reads 10 characters, starting at the
current input position, while the second writes the eleven
characters of “some data” to the file, without an end-of-file
marker afterwards.

It is possible to reposition character-wise too, some examples
are:

     call charin ‘myfile’,, 8
     call charout ‘foofile,, 10

These two clauses repositions the current read and write positions
of the named files to the 8th and 10th characters, respectively.




PREV NEXT