File Status Example


Scenario - Declaring a file status variable and using to handle the errors.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FLSTATUS.
       AUTHOR. MTH.
	   
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
      * EMPFILE Definition
           SELECT EMPFILE ASSIGN TO INPUT01
           ORGANIZATION IS INDEXED 
           ACCESS MODE  IS DYNAMIC
           RECORD KEY   IS EMP-ID
           FILE STATUS  IS WS-FS1.

       DATA DIVISION.
       FILE SECTION.
      * EMPFILE structure definition       
       FD EMPFILE
           RECORD CONTAINS 47  CHARACTERS
           BLOCK  CONTAINS 470 CHARACTERS
           DATA RECORD     IS EMPFILE-RECORD.
		   
      * EMPFILE record structure definition 
       01 EMPFILE-RECORD.
          05 EMP-ID      PIC X(05).
          05 EMP-NAME    PIC X(14).
          05 EMP-DESG    PIC X(05).
          05 EMP-DEPT    PIC X(05).
          05 EMP-LOC     PIC X(05).
          05 EMP-SALARY  PIC 9(10).
          05 FILLER      PIC X(03).

       WORKING-STORAGE SECTION.
	   01 WS-VAR.
          05 WS-FS1      PIC X(02) VALUE ZERO.

       PROCEDURE DIVISION.
      * Opens the file for reading
           OPEN INPUT EMPFILE.

      * Performs READ operation when File open is successful		   
           PERFORM UNTIL WS-FS1 NOT = '00'
                READ EMPFILE 
                     NOT AT END DISPLAY EMPFILE-RECORD
                END-READ
           END-PERFORM.

	  * Closing the opened file
           CLOSE EMPFILE. 
		   
           STOP RUN.

Explaining Example -

In the above example:

  • The file status (WS-FS1) is typically a two-character code.
  • The first character is the general code, and the second provides further detailed information.