Defining a File Status Data Item

Each file in a program can have a file status data item associated with it by specifying FILE STATUS IS in the SELECT clause for the file and defining the file status data item in the Working-Storage Section:

 select in-file
     assign to "user.dat"
     file status is ws-file-status.
 working-storage section.
     01 ws-file-status        pic xx.

A separate data item can be used for each file or a single data item can be used for multiple files.

File status is a two-byte code. For all file status conventions, except extended file status codes, the data item is defined as two alphanumeric characters (PIC XX). For extended file status codes, the second byte contains a binary number.

The first byte of the file status data item is known as status key 1 and indicates one of the following conditions upon completion of an I/O operation:

Value Condition
0 Successful operation
2 Invalid key
3 Permanent error
4 Logic error (improper sequence of I/O operations)
9 COBOL run-time system error message

If the first byte of the file status data item is other than 9, the second byte (known as status key 2) is an alphanumeric character.

If the first byte of the file status data item is 9, the second byte is a binary field containing a run-time system error code.

The example code that follows illustrates how file status checking is performed: first the first byte (status key 1) is interrogated; then, if more information is required, the second byte (status key 2) is interrogated.

     select recseq
         assign to "recseq.dat"
         file status is ws-file-status
         organization is record sequential.
 file section.
 fd recseq
     record contains 80 characters.
 01 recseq-fd-record    pic x(80).
 working-storage section.
 01 ws-file-status.
     05 status-key-1     pic x.
     05 status-key-2     pic x.
     05 binary-status redefines status-key-2  pic 99 comp-x.
 procedure division.
     perform check-status.
     evaluate status-key-1
      when "0" 
         next sentence
      when "1" 
         display "end of file reached"
         perform check-eof-status
      when "2" 
         display "invalid key"
         perform check-inv-key-status
      when "3" 
         display "permanent error"
         perform check-perm-err-status
      when "4" 
         display "logic error"
      when "9" 
         display "run-time-system error"
         perform check-mf-error-message
     if status-key-2 = "0"
         display "no next logical record"
     evaluate status-key-2
        when "2" display "attempt to write dup key"
        when "3" display "no record found"
     if status-key-2 = "5"
         display "file not found"
     evaluate binary-status
      when 002 display "file not open       "
      when 007 display "disk space exhausted"
      when 013 display "file not found      "
      when 024 display "disk error          "
      when 065 display "file locked         "
      when 068 display "record locked       "
      when 039 display "record inconsistent "
      when 146 display "no current record   "
      when 180 display "file malformed      "
      when 208 display "network error       "
      when 213 display "too many locks      "
      when other 
         display "not error status " 
         display binary-status