Chapter 8. Input and Output

Table of Contents
More about Stream Objects
Reading a Text File
Reading a Text File into an Array
Reading Specific Lines of a Text File
Writing a Text File
Reading Binary Files
Reading Text Files a Character at a Time
Writing Binary Files
Closing Files
Direct File Access
Checking for the Existence of a File
Getting Other Information about a File
Using Standard I/O
Using Windows Devices

Object Rexx supports a stream I/O model. Using streams, your program reads data from various devices (such as hard disks, CD-ROMs, and keyboards) as a continuous stream of characters. Your program also writes data as a continuous stream of characters.

In the stream model, a text file is represented as a stream of characters with special new-line characters marking the end of each line of text in the stream. A binary file is a stream of characters without an inherent line structure. A Rexx stream object allows you read from a data stream using either the text-file line methods or using a continous data stream method.

The Rexx Stream class is the mechanism for accessing I/O streams. To input or output data, you first create an instance of the Stream class that represents the device or file you want to use. For example, the following clause creates a stream object for the file C:\out.dat:

/* Create a stream object for out.dat */
file=.stream~new("c:\out.dat")

Then you use the appropriate stream methods access the data. OUT.DAT is a text file, so you would normally use the LINES(), LINEIN, and LINEOUT() methods that that read or write data as delimited lines. If the stream represents a binary file (such as a WAV, GIF, TIF, AVI, or EXE file), you would use the CHAR, CHARIN, and CHAROUT methods that read and write data as characters.

The Stream class includes additional methods for opening and closing streams, flushing buffers, seeking, retrieving stream status, and other input/output operations.

More about Stream Objects

To use streams in Rexx, you create new instances of the Stream class. These stream objects represent the various data sources and destinations available to your program, such as hard disks, CD-ROMs, keyboards, displays, printers, serial interfaces, network.

Stream objects can be transient or persistent. An example of a transient (or dynamic) stream object is a serial interface. Data can be sent or received from serial interfaces, but the data is not stored permanently by the serial interface itself. Consequently, you cannot, for example,read from a random position in the data stream—it can only be read as a sequential stream of characters. Once you write to the stream, the data cannot be read again.

A disk file is an example of a persistent stream object. Because the data is stored on disk, you can search forward and backward in the stream and reread data that you have previously read. Rexx maintains separate read and write pointers to a stream that you can move the pointers independently using arguments on methods such as LINEIN, LINEOUT, CHARIN, and CHAROUT. The Stream class also provides SEEK and POSITION methods for setting the read and write positions.