Menu
CoddyTech

FILE STATUS

Part of the Files and Reports section of Coddy's COBOL Programming journey. Lesson 5 of 22.

FILE STATUS IS names a two-character field that gets a code after every OPEN, READ, WRITE and CLOSE on the file:

    SELECT CUST-FILE ASSIGN TO CUSTIN
        ORGANIZATION IS LINE SEQUENTIAL
        FILE STATUS IS WS-STATUS.
01 WS-STATUS PIC XX.

The codes to know: 00 the statement worked; 10 a READ found the end of the file; 35 OPEN INPUT found no data set; 47 a READ on a file that is not open.

Without FILE STATUS, a failed OPEN stops the program with an error. With it, the program keeps running and checks the code itself:

IF WS-STATUS NOT = "00"
    DISPLAY "Open failed: " WS-STATUS
    STOP RUN
END-IF.

An unchecked failed OPEN is worse than a stop: every READ then sets 47, never AT END, and a read loop runs forever.

challenge icon

Challenge

Easy

The data set RATEIN is missing, so the program stops with an error. Add FILE STATUS IS WS-STATUS to the SELECT. After the OPEN, when WS-STATUS is not "00", display Cannot open RATEIN: 35 (the status) and stop.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FSTAT.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT RATE-FILE ASSIGN TO RATEIN
               ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       FILE SECTION.
       FD RATE-FILE.
       01 RATE-REC PIC X(20).
       WORKING-STORAGE SECTION.
       01 WS-STATUS PIC XX.
       PROCEDURE DIVISION.
           OPEN INPUT RATE-FILE.
           *> Check WS-STATUS here
           READ RATE-FILE.
           DISPLAY RATE-REC.
           CLOSE RATE-FILE.
           STOP RUN.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Files and Reports