Datasets, DD names and FDs
Until now, your programs have read from the keyboard and written to the screen. Real batch programs read and write datasets: files of records, processed from the first record to the last. This module is about the simplest and most common kind, the sequential file.
The program never names the dataset
On z/OS a dataset has a name such as PAYROLL.EMPLOYEES.MASTER. You will
not find that name in a well-written COBOL program. Instead the program
refers to a short DD name, and the job that runs the program connects
the two:
//EMPIN DD DSN=PAYROLL.EMPLOYEES.MASTER,DISP=SHR
That line of JCL (you will learn JCL properly later) says: for this run,
DD name EMPIN means dataset PAYROLL.EMPLOYEES.MASTER. Tomorrow's job
can point EMPIN at a test copy without recompiling anything. This
indirection is why the same program can run against test and production
data.
Here, each exercise's test does the same job with an environment
variable: DD_EMPIN=PAYROLL.EMPLOYEES.MASTER. GnuCOBOL looks up
DD_ plus the DD name when the file is opened. The exercise prompt always
tells you which DD names to use.
Three pieces of code per file
- SELECT, in the ENVIRONMENT DIVISION, gives the file an internal name and says which DD name it is assigned to, how it is organised, and (next lessons) where to put its status code.
- FD (file description), in the FILE SECTION of the DATA DIVISION, describes the file and is followed by the record layout.
- Verbs in the PROCEDURE DIVISION:
OPEN,READorWRITE,CLOSE.
IDENTIFICATION DIVISION.
PROGRAM-ID. NEWEMPS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMP-FILE ASSIGN TO EMPOUT
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD EMP-FILE.
01 EMP-REC.
05 EMP-ID PIC X(5).
05 EMP-NAME PIC X(20).
05 EMP-DEPT PIC X(3).
PROCEDURE DIVISION.
OPEN OUTPUT EMP-FILE
MOVE "E0001" TO EMP-ID
MOVE "GRACE HOPPER" TO EMP-NAME
MOVE "ENG" TO EMP-DEPT
WRITE EMP-REC
MOVE "E0002" TO EMP-ID
MOVE "ALAN TURING" TO EMP-NAME
MOVE "RES" TO EMP-DEPT
WRITE EMP-REC
CLOSE EMP-FILE
DISPLAY "2 RECORDS WRITTEN"
STOP RUN.
Run with DD_EMPOUT=HR.EMPLOYEES, this displays 2 RECORDS WRITTEN, and
dataset HR.EMPLOYEES contains:
E0001GRACE HOPPER ENG
E0002ALAN TURING RES
The record area
The 01 EMP-REC under the FD is not the file. It is a buffer for one
record. You fill it and WRITE EMP-REC sends it to the file. When you
read, READ EMP-FILE fills it with the next record. Note the asymmetry
that trips everyone up: you READ a file but WRITE a record.
OPEN modes
| Mode | Use |
|---|---|
OPEN INPUT |
read an existing file from the start |
OPEN OUTPUT |
create the file, replacing anything that was there |
OPEN EXTEND |
add records to the end of an existing file |
OPEN I-O |
read records and rewrite them in place |
CLOSE finishes the file: it flushes buffered records to disk. Always
close every file you open before STOP RUN.
LINE SEQUENTIAL
ORGANIZATION IS LINE SEQUENTIAL means a text file with one record per
line, so you can read the datasets and diffs in your browser. Trailing
spaces are dropped when a record is written and restored when it is read.
Mainframe datasets are different: fixed-length records with no line
breaks. You will work with those later in this module.
On the job
When a batch job fails, the first thing people check is the DD
statements: is EMPIN pointing at the right generation of the right
dataset? Knowing that the program only sees DD names makes many
"it worked in test" mysteries easy to solve.