Mainframe > Tips & Techniques > Application Development

A Beginner's Guide to the REXX Programming Language on z/OS

Reading and Writing Files in the REXX programming language on z/OS.

Reading and Writing Files in the REXX programming language on z/OS.

To learn more about this topic, read these articles: 
REXX Talks
REXX Programmers Shouldn't Overlook 'Stacks'
Schedule Tasks Using Cron and REXX
REXX Routines for Easy Updates or Generating Workload
Leveraging SMF Data With (or Without) REXX
REXX Offers Tools for Automated Processing of SMF Data
Processing SMF Records Using REXX
A Closer Look at the REXX Code for Processing SMF Data

In this series of articles, I’ll provide a beginner’s guide to the REXX* programming language on z/OS*. Let’s head straight into reading and writing files.

Getting Started

Reading z/OS data sets in REXX requires two steps. First, allocate the data set—this is a Time Sharing Option (TSO) command. Second, process the records in the data set—this is implemented as a TSO REXX command. The commands are, in sequence:

"alloc shr file(input) dataset(‘MY.MAINFRAME.
DATASET’)"
"execio * diskr input (stem input. finis)"

The first command uses the syntax of the TSO ALLOCATE command. Under TSO, try the command HELP ALLOCATE SYNTAX for a quick overview of the command parameters.

The second command is a little trickier. See Code Sample 1 for a quick description of the full command syntax I’ve used here. For any REXX-platform-specific implementation, there’s a dedicated I/O processing capability. Under TSO, this is implemented with the “execio” command. There are many variations on this command, some more of which I’ll explore in future articles. In this case, you’ve read the entire file into a “buffer” in one pass. The buffer is technically referred to as a “stem variable,” and I’ll use this term from now on. The stem variable specified in the above execio statement is named “input.” The trailing dot character, while not essential, is highly recommended. Process the contents of the stem variable separately. Close the file automatically after reading it so, if desired, you can now issue the TSO FREE command like this:

"free file(input)"

Since you read the entire file into a buffer, you now need some way of processing each record in sequence from the stem variable. To do this, take advantage of the fact that reading the file in this manner has automatically updated a special stem variable with the number of records read. This is the stem “zero” variable and is addressed as “stem.0” in REXX. The value of the input.0 variable is used in this case to determine the upper limit for a “do while” loop to process all records in the stem variable. Records in the stem variable are addressed by record number using the syntax stem.record_num, or input.count in the code below.

To bring these code snippets together into a functional program, you need a little bit of glue around them. I’ve added a simple loop that demonstrates processing each record from the stem variable in sequence:

Michael Cairns works for IBM as a technical specialist in the Tivoli zSecure range of software. Michael can be reached at mike.cairns@au1.ibm.com.


comments powered by Disqus
Buyers Guide

Advertisement

A Beginner's Guide to the REXX Programming Language on z/OS

Reading and Writing Files in the REXX programming language on z/OS.

MAINFRAME > TIPS & TECHNIQUES > APPLICATION DEVELOPMENT

Application Management is Important to the Entire Process

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
AIX News Sign Up Today! Past News Letters

Advertisement