COBOL file status

File status 42: CLOSE of a file that isn't open

File status 42 means the program issued CLOSE for a file that isn't open. Most often the OPEN failed earlier and the program carried on without checking it.

Other names for this code

Also written as FS 42, file status code 42, COBOL status 42, status key 42, CLOSE file not open COBOL, SK42.

What it means

Status 42 is a logic error. COBOL only allows CLOSE on a file that's open. If the file was never opened, or was already closed, CLOSE fails with 42.

The runtime checks the file's open state itself, so 42 looks the same for QSAM, VSAM and line-sequential files. Under the older CMPR2 behavior it was reported as 92.

Treat 42 as a clue rather than the real problem. It usually turns up in the end-of-job routine, after something else has gone wrong. The useful question is why the file wasn't open at that point, and the answer is often a failed OPEN a few lines earlier in the job log.

Common causes

  • The OPEN failed, for example with 35 or 39. The program didn't check the status, and the end-of-job routine closed every file anyway.
  • The file is only opened on some paths through the program, but the termination routine closes it on all of them.
  • The file was closed twice, once in an error routine and again in normal termination.
  • Another program in the run unit, with its own FD for the same ddname, tries to close a file it never opened.

How to find the problem

  1. Go back to the OPEN for the same file and check its status. If you don't log it, add a DISPLAY of the status after every OPEN.
  2. Look earlier in the job log for runtime messages about the same file.
  3. Trace the path into the termination routine to see which branch skipped the OPEN or had already closed the file.

How to fix it

  • Check the status after every OPEN and stop with a clear message if it fails, instead of carrying on.
  • Keep an open flag for each file and only CLOSE files whose flag is set.
  • Route all error paths through one termination routine, and make sure it runs once.

IBM documentation

IBM Enterprise COBOL documentation. Use the documentation for your installed release.

This link opens the relevant IBM reference library or lookup instructions. Match the complete code and your installed product release; it is not a code-specific review.

Report a correction to WND Software.

Questions

Is file status 42 serious?

On its own it does no harm, but it usually hides an earlier problem, most often a failed OPEN. Find that first.

What is the difference between 41 and 42?

41 is OPEN on a file that's already open. 42 is CLOSE on a file that isn't open.

Why do I get 42 when the program clearly opens the file?

Either the OPEN failed and left the file closed, or a different program in the run unit is issuing the CLOSE against its own copy of the file.