TExcelTAD
Home › COBOL › COBOL File Status & File Types

COBOL File Status & File Types

Every file operation can fail: the file is missing, a disk is full, a key is duplicated. COBOL reports the outcome of each OPEN, READ, WRITE, REWRITE and CLOSE in a two-character file status field that you name in the SELECT entry. Checking it is what separates a robust batch program from one that silently produces wrong output.

Common file status codes

Status Meaning
00 Success
02 Success, duplicate alternate key (indexed files)
04 Record length does not match the description
05 OPEN of an OPTIONAL file that does not exist (the file is treated as empty)
10 End of file
22 Duplicate key on WRITE (indexed/relative files)
23 Record not found (keyed READ)
30 Permanent I/O error
35 OPEN INPUT on a file that does not exist
41 / 42 File already open / not open
46 READ after end of file was already reached
47 / 48 READ / WRITE not allowed in the current open mode

The first character gives the class: 0 success, 1 end of file, 2 key problem, 3 permanent error, 4 logic error in the program.

Checking the status

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. STATUS-CHECK.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT MISSING-FILE ASSIGN TO "no-such-file.dat"
               ORGANIZATION IS LINE SEQUENTIAL
               FILE STATUS IS WS-STATUS.
       DATA DIVISION.
       FILE SECTION.
       FD  MISSING-FILE.
       01  MISSING-RECORD         PIC X(20).
       WORKING-STORAGE SECTION.
       01  WS-STATUS              PIC XX.
           88  STATUS-OK                      VALUE "00".
           88  STATUS-NOT-FOUND               VALUE "35".
       PROCEDURE DIVISION.
           OPEN INPUT MISSING-FILE
           EVALUATE TRUE
               WHEN STATUS-OK
                   DISPLAY "File opened"
                   CLOSE MISSING-FILE
               WHEN STATUS-NOT-FOUND
                   DISPLAY "Input file is missing (status 35)"
               WHEN OTHER
                   DISPLAY "Unexpected status " WS-STATUS
           END-EVALUATE
           READ MISSING-FILE
               AT END DISPLAY "at end"
           END-READ
           DISPLAY "READ on a file that is not open: " WS-STATUS
           STOP RUN.
Input file is missing (status 35)
READ on a file that is not open: 47

The READ after the failed OPEN returns 47: the file is not open for input, so reading is not allowed.

When a FILE STATUS clause is present, an I/O error does not stop the program: it is your job to look at the status. Without the clause (and without a USE declarative), GnuCOBOL stops the run with an error message on most failures.

Updating records in place with REWRITE

A file opened I-O can be read and its records changed with REWRITE, which replaces the record that was just read. For sequential files the record length must stay the same. Here every balance gets 2% interest:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. REWRITE-DEMO.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT ACCOUNTS ASSIGN TO "accounts.dat"
               ORGANIZATION IS SEQUENTIAL
               FILE STATUS IS WS-STATUS.
       DATA DIVISION.
       FILE SECTION.
       FD  ACCOUNTS.
       01  ACCOUNT-RECORD.
           05  ACC-NUMBER         PIC 9(4).
           05  ACC-BALANCE        PIC 9(6)V99.
       WORKING-STORAGE SECTION.
       01  WS-STATUS              PIC XX.
       01  WS-EOF                 PIC X       VALUE "N".
       PROCEDURE DIVISION.
           OPEN OUTPUT ACCOUNTS
           MOVE 1 TO ACC-NUMBER  MOVE 1000.00 TO ACC-BALANCE
           WRITE ACCOUNT-RECORD
           MOVE 2 TO ACC-NUMBER  MOVE 250.50 TO ACC-BALANCE
           WRITE ACCOUNT-RECORD
           CLOSE ACCOUNTS

           OPEN I-O ACCOUNTS
           PERFORM UNTIL WS-EOF = "Y"
               READ ACCOUNTS
                   AT END MOVE "Y" TO WS-EOF
                   NOT AT END
                       COMPUTE ACC-BALANCE ROUNDED = ACC-BALANCE * 1.02
                       REWRITE ACCOUNT-RECORD
                       DISPLAY "Rewrote " ACC-NUMBER ": " ACC-BALANCE
                               " status " WS-STATUS
               END-READ
           END-PERFORM
           CLOSE ACCOUNTS
           STOP RUN.
Rewrote 0001: 001020.00 status 00
Rewrote 0002: 000255.51 status 00

This file uses plain ORGANIZATION IS SEQUENTIAL: fixed-length 12-byte records with no newlines, the format of most mainframe data sets.

File organisations

Organisation Access Like
SEQUENTIAL In order only A tape or a flat file
LINE SEQUENTIAL In order, text lines (extension) A text file
RELATIVE By record number, or in order An array on disk
INDEXED By key, or in key order A keyed table, like a simple database (VSAM KSDS on a mainframe)

Indexed files need an indexing library at run time. The GnuCOBOL build on the playground has none, so indexed examples are shown here without a Run button. On your own machine, GnuCOBOL built with Berkeley DB or VBISAM runs them as written:

cobol
           SELECT CUSTOMER-MASTER ASSIGN TO "custmast.idx"
               ORGANIZATION IS INDEXED
               ACCESS MODE IS DYNAMIC
               RECORD KEY IS CM-CUST-ID
               ALTERNATE RECORD KEY IS CM-NAME WITH DUPLICATES
               FILE STATUS IS WS-CM-STATUS.
      *> ...
           MOVE 1002 TO CM-CUST-ID
           READ CUSTOMER-MASTER
               INVALID KEY
                   DISPLAY "Customer 1002 not found"
               NOT INVALID KEY
                   DISPLAY "Found " CM-NAME
           END-READ

           MOVE 1000 TO CM-CUST-ID
           START CUSTOMER-MASTER KEY IS >= CM-CUST-ID
           READ CUSTOMER-MASTER NEXT RECORD
               AT END SET END-OF-FILE TO TRUE
           END-READ
  • ACCESS MODE is SEQUENTIAL, RANDOM (by key) or DYNAMIC (both).
  • READ … INVALID KEY handles a key that does not exist (status 23).
  • START positions the file at a key; READ … NEXT then reads forward in key order.
  • WRITE, REWRITE and DELETE work by key; a duplicate key on WRITE gives status 22.

Error handling with declaratives

Instead of checking the status after every statement, a program can define a DECLARATIVES section with USE AFTER ERROR PROCEDURE ON file. It runs automatically when an I/O error occurs on that file:

cobol
       PROCEDURE DIVISION.
       DECLARATIVES.
       CUSTOMER-ERROR SECTION.
           USE AFTER STANDARD ERROR PROCEDURE ON CUSTOMER-FILE.
           DISPLAY "I/O error on CUSTOMER-FILE, status " WS-CUST-STATUS
           MOVE 16 TO RETURN-CODE.
       END DECLARATIVES.

       MAIN-LOGIC SECTION.
           ...

Test yourself

What does file status 35 mean?

  1. End of file
  2. Duplicate key
  3. OPEN INPUT on a file that does not exist
  4. Success

What does file status 10 mean after a READ?

  1. Record not found
  2. End of file reached
  3. Permanent error
  4. Success

Which statement replaces the record just read from a file opened I-O?

  1. WRITE
  2. UPDATE
  3. REWRITE
  4. REPLACE