FILE STATUS Codes


COBOL file status codes provide information about the results of each file operation executed. They're mainly useful for error handling, giving programmers a straightforward way to identify issues with file operations. When a file operation like OPEN, READ, WRITE, etc., is executed, a status code is returned to the variable coded with the FILE STATUS clause.

For example -

      ...
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           ... 
           FILE STATUS  IS WS-FS1.

       DATA DIVISION.
       ...
       WORKING-STORAGE SECTION.
       01 WS-FS1        PIC X(02) VALUE ZERO.

       PROCEDURE DIVISION.
           OPEN INPUT EMPFILE. 
           PERFORM UNTIL WS-FS1 NOT = '00'
                READ EMPFILE 
                     NOT AT END DISPLAY EMPFILE-RECORD
                END-READ
           END-PERFORM.
           CLOSE EMPFILE. 
		   
           STOP RUN.

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.

The status code values and their meanings are shown in the below table -

Higher digit MeaningLower digitMeaning
0 Successful completion0No error occurred during the operation.
2READ operation is successful, but duplicate key found. The file operation was successful, but a duplicate key was found. This file status is applicable only to indexed files with alternate keys that allow duplicates.
4A READ operation was successful. However, the length of the retrieved record was larger or shorter than the file record structure coded. This happens when RECORD IS VARYING caluse is coded.
5File OPEN is successful. However, the optional file was unavailable when the OPEN statement was executed.
1 End of file (EOF)0A sequential READ was attempted, and no next logical record existed in the file because the end of the file was reached.
OR
The first READ was attempted on an optional input file that was unavailable.
4A sequential READ was attempted on a relative file, and the RRN size was larger than the relative key variable coded for the file.

Solution - Check the size of the of relative key variable and modify according to RRN size.
2 Invalid key related0A key-related operation (e.g., a READ or START on an indexed file) has been attempted with a key value that doesn't exist in the file.

Solution - Check the logic and handle the error.
1A sequential READ is successful. The program changes the record key between the successful execution of a READ statement and the execution of the next REWRITE or DELETE statement.

Solution - Check the program logic and avoid changing the key value between READ and REWRITE or DELETE.
2Attempted to WRITE or REWRITE a record with a key value that already exists in the file.

Solution - Check the record existance before writing the record.
3Attempted a READ or DELETE with a key value that doesn't exist.
OR
A START or random READ statement was attempted on an optional input file that was unavailable.

Solution - Code the error handling to bypass error.
4An attempt was made to write beyond the boundaries of a relative or indexed file.
OR
A sequential WRITE was attempted for a relative file, and the number of significant digits in the RRN was larger than the size of the relative key variable coded for the file

Solution - Check the value of the key for the boundary values and correct it.
3 Permanent error condition
4 Encountered on a relative file. Attempted to READ or WRITE outside the boundaries of the file.

Solution - Check the value of the record and correct it.
5An OPEN with the INPUT, I-O, or EXTEND phrase was attempted on a non-optional file that was unavailable.

Solution - Check and correct the file name.
7An OPEN statement was attempted on a file that would not support the open-mode specified in the OPEN statement. Possible violations are -
  • The EXTEND or OUTPUT phrase was specified but the file would not support write operations.
  • The I-O phrase was specified but the file would not support the input and output operations.
  • The INPUT phrase was specified but the file would not support read operations.


Solution - Check for the file opened mode and code the file operations accordingly.
8An OPEN statement was attempted on a file previously closed with lock.

Solution - Release the file lock and open the file.
9File OPEN was unsuccessful because a conflict was detected between the actual file attributes and the attributes specified for that file in the program.

Solution - Check the file actual attributes and modify the attributes in the program.
4 Logic error condition1Attempted to OPEN a file that's already open.

Solution - Check the logic and remove the second OPEN statement in the program.
2A CLOSE statement was attempted for a file not in the open mode.

Solution - Check the logic and correct it.
3For a file in the sequential access mode, the last READ before the execution of a REWRITE was not successful.
For relative and indexed files in the sequential access mode, the READ before the execution of a DELETE or REWRITE was unsuccessful.

Solution - Check why the READ is not successful and try avoiding REWRITE or DELETE when READ is not successful.
4An attempt was made to REWRITE a record and the record was not the same size as the record being replaced.

Solution - Check the record length why is not in the same size.

OR
An attempt was made to WRITE or REWRITE a record that was larger than the largest or smaller than the smallest record allowed by the RECORD IS VARYING of the associated file.

Solution - Check and provide the suitable record layout.
6A sequential READ statement was attempted on a file open in the INPUT or I-O mode and no valid next record because -
  • The preceding READ was unsuccessful but did not cause an at-end condition.
  • The preceding READ caused an at-end condition.


Solution - Check the logic and restrict the READ operation on preceding READ is not successful.
7A READ was attempted on a file not open in the INPUT or I-O mode.

Solution - Check the logic and open the file in INPUT or I-O mode before READ operation.
8A WRITE statement was attempted on a file not open in the I-O, OUTPUT or EXTEND mode.

Solution - Check the logic and open the file in I-O, OUTPUT or EXTEND mode before WRITE operation.
9A DELETE or REWRITE statement was attempted on a file not open in the I-O mode.

Solution - Check the logic and open the file in I-O mode before DELETE or REWRITE operation.
9 Implementor-defined condition0No further information
1Authorization failure
2Logic error
3Resource unavailable
4Concurrent open error
5Invalid or incomplete file information
6File system unavailable
8Open failed due to locked file
9Record access failed due to locked record

Resolutions to avoid file errors -


  • Always Check File Status: After each file operation, always check the file status. This will ensure you catch any errors immediately after they occur.
  • For Key-Related Errors: Ensure the key fields are correctly initialized and used. Check for duplicates and the sequence of keys, especially in indexed files.
  • For File Structural Errors: Verify the file's structure matches what the program expects. This may require tools or utilities specific to your system or file type.
  • Access Modes: Make sure you're using the correct access mode for your operation, especially with indexed and relative files.
  • File Availability: Ensure the file exists, is accessible (not locked by another user/process), and has the correct permissions.
  • Handle EOF Gracefully: Anticipate the end-of-file status and handle it gracefully, especially in loops where you're reading records sequentially.
  • For File Already Open: Ensure the file is closed before trying to open it again.