COBOL Sequential Files
Processing files of records is what COBOL was built for. A sequential file is read from the first record to the last, in order, like a tape. Batch programs typically read an input file, process each record and write an output file or report.
Working with a file takes four pieces:
| Where | What |
|---|---|
ENVIRONMENT DIVISION, FILE-CONTROL |
SELECT file ASSIGN TO "name": connect the file to a real file |
DATA DIVISION, FILE SECTION |
FD file + an 01 record layout: the record buffer |
| PROCEDURE DIVISION | OPEN, READ / WRITE, CLOSE |
| WORKING-STORAGE | A FILE STATUS field and an end-of-file flag |
The playground runs each program in a temporary directory, so the examples first write a small file and then read it back.
Writing a file
IDENTIFICATION DIVISION.
PROGRAM-ID. WRITE-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUSTOMER-FILE ASSIGN TO "customers.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-FILE-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUSTOMER-FILE.
01 CUSTOMER-RECORD.
05 CUST-ID PIC 9(4).
05 CUST-NAME PIC X(12).
05 CUST-BALANCE PIC 9(5)V99.
WORKING-STORAGE SECTION.
01 WS-FILE-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT CUSTOMER-FILE
DISPLAY "OPEN status: " WS-FILE-STATUS
MOVE 1001 TO CUST-ID
MOVE "Ana Silva" TO CUST-NAME
MOVE 1520.75 TO CUST-BALANCE
WRITE CUSTOMER-RECORD
MOVE 1002 TO CUST-ID
MOVE "Ben Okoro" TO CUST-NAME
MOVE 88.10 TO CUST-BALANCE
WRITE CUSTOMER-RECORD
DISPLAY "WRITE status: " WS-FILE-STATUS
CLOSE CUSTOMER-FILE
DISPLAY "Two records written"
STOP RUN.OPEN status: 00
WRITE status: 00
Two records written
ORGANIZATION IS LINE SEQUENTIALis a GnuCOBOL/Micro Focus extension for text files: one record per line, ending in a newline. PlainORGANIZATION IS SEQUENTIALwrites fixed-length records with no line breaks, which is what mainframe data sets look like.OPEN OUTPUTcreates the file (replacing any old one).OPEN EXTENDappends to it.WRITEtakes the record name, not the file name: "read a file, write a record".- The file status
"00"means success.
Reading a file until end of file
The standard reading loop uses READ … AT END to set a flag, and PERFORM UNTIL the flag is set. The program
below writes three records and then reads them back:
IDENTIFICATION DIVISION.
PROGRAM-ID. READ-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SALES-FILE ASSIGN TO "sales.dat"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD SALES-FILE.
01 SALES-RECORD.
05 SALE-REGION PIC X(6).
05 SALE-AMOUNT PIC 9(5)V99.
WORKING-STORAGE SECTION.
01 WS-EOF PIC X VALUE "N".
88 END-OF-FILE VALUE "Y".
01 WS-COUNT PIC 99 VALUE 0.
01 WS-TOTAL PIC 9(6)V99 VALUE 0.
PROCEDURE DIVISION.
PERFORM CREATE-TEST-FILE
OPEN INPUT SALES-FILE
PERFORM UNTIL END-OF-FILE
READ SALES-FILE
AT END
SET END-OF-FILE TO TRUE
NOT AT END
ADD 1 TO WS-COUNT
ADD SALE-AMOUNT TO WS-TOTAL
DISPLAY SALE-REGION " " SALE-AMOUNT
END-READ
END-PERFORM
CLOSE SALES-FILE
DISPLAY WS-COUNT " records, total " WS-TOTAL
STOP RUN.
CREATE-TEST-FILE.
OPEN OUTPUT SALES-FILE
MOVE "NORTH 0012000" TO SALES-RECORD
WRITE SALES-RECORD
MOVE "SOUTH 0004550" TO SALES-RECORD
WRITE SALES-RECORD
MOVE "EAST 0020125" TO SALES-RECORD
WRITE SALES-RECORD
CLOSE SALES-FILE.NORTH 00120.00
SOUTH 00045.50
EAST 00201.25
03 records, total 000366.75
MOVE "NORTH 0012000" TO SALES-RECORD fills the whole record at once: 6 characters of region, then the 7 digits
0012000, which the 9(5)V99 field reads as 120.00. That is exactly what a line in a real input file looks like.
Note: After
AT END, the record area no longer holds valid data. Never use the record fields after the end-of-file condition.
The priming read pattern
Many shops write the loop with a priming read: read the first record before the loop, process it, and read
the next record at the end of each pass. It avoids the NOT AT END branch:
OPEN INPUT SALES-FILE
PERFORM READ-SALE
PERFORM UNTIL END-OF-FILE
PERFORM PROCESS-SALE
PERFORM READ-SALE
END-PERFORM
CLOSE SALES-FILE
...
READ-SALE.
READ SALES-FILE
AT END SET END-OF-FILE TO TRUE
END-READ.READ INTO and WRITE FROM
READ file INTO ws-item reads a record and copies it to a working-storage item; WRITE record FROM ws-item copies
before writing. Working on a copy keeps the data available after the next READ and lets several files share one
layout. Here a filter program copies only large orders to a second file:
IDENTIFICATION DIVISION.
PROGRAM-ID. FILTER-ORDERS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ORDERS-IN ASSIGN TO "orders.dat"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT ORDERS-BIG ASSIGN TO "big-orders.dat"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD ORDERS-IN.
01 IN-RECORD PIC X(14).
FD ORDERS-BIG.
01 BIG-RECORD PIC X(14).
WORKING-STORAGE SECTION.
01 WS-ORDER.
05 ORD-NUMBER PIC 9(5).
05 FILLER PIC X.
05 ORD-AMOUNT PIC 9(6)V99.
01 WS-EOF PIC X VALUE "N".
PROCEDURE DIVISION.
OPEN OUTPUT ORDERS-IN
WRITE IN-RECORD FROM "10001 00005000"
WRITE IN-RECORD FROM "10002 00250000"
WRITE IN-RECORD FROM "10003 00012500"
WRITE IN-RECORD FROM "10004 01000000"
CLOSE ORDERS-IN
OPEN INPUT ORDERS-IN OUTPUT ORDERS-BIG
PERFORM UNTIL WS-EOF = "Y"
READ ORDERS-IN INTO WS-ORDER
AT END MOVE "Y" TO WS-EOF
NOT AT END
IF ORD-AMOUNT >= 1000
WRITE BIG-RECORD FROM WS-ORDER
END-IF
END-READ
END-PERFORM
CLOSE ORDERS-IN ORDERS-BIG
MOVE "N" TO WS-EOF
OPEN INPUT ORDERS-BIG
PERFORM UNTIL WS-EOF = "Y"
READ ORDERS-BIG INTO WS-ORDER
AT END MOVE "Y" TO WS-EOF
NOT AT END
DISPLAY "Big order " ORD-NUMBER ": " ORD-AMOUNT
END-READ
END-PERFORM
CLOSE ORDERS-BIG
STOP RUN.Big order 10002: 002500.00
Big order 10004: 010000.00
OPEN modes
| Mode | Allows | Notes |
|---|---|---|
INPUT |
READ |
File must exist |
OUTPUT |
WRITE |
Creates or empties the file |
EXTEND |
WRITE at the end |
Appends |
I-O |
READ, REWRITE (and DELETE for indexed files) |
Update in place |
Test yourself
Which statement is correct?
- WRITE CUSTOMER-FILE
- WRITE CUSTOMER-RECORD
- READ CUSTOMER-RECORD
- OPEN CUSTOMER-RECORD
What does OPEN OUTPUT do to an existing file?
- Appends to it
- Replaces its contents
- Fails
- Opens it for reading
Where is the record layout of a file described?
- In FILE-CONTROL
- In the FD entry in the FILE SECTION
- In LINKAGE SECTION
- In the SELECT clause