COBOL file status

File status 41: OPEN of a file that is already open

File status 41 means the program issued OPEN for a file that is already open. The second OPEN fails, and the file stays open as it was.

Other names for this code

Also written as FS 41, file status code 41, COBOL status 41, status key 41, file already open COBOL, SK41.

What it means

Status 41 is a logic error. The 4x class means the program ran statements in an order COBOL doesn't allow. Nothing is wrong with the data set itself. Here the program reached an OPEN for a file it had already opened and not closed.

The COBOL runtime keeps track of whether each file is open, and the check works the same for QSAM, VSAM and line-sequential files. Under the older CMPR2 behavior this case was reported as 92. If the program has no FILE STATUS clause and no error declarative, the runtime treats it as a severe error and ends the program.

A 41 is never about another job holding the file. That's a sharing problem, which shows up as status 93 for VSAM or as a wait at allocation.

Common causes

  • A called subprogram opens its file every time it's called. The first call works and the second gets 41.
  • An initialization paragraph runs twice, for example when a restart path or a loop PERFORMs it again.
  • The program opens a file INPUT to read it, then opens it OUTPUT to rewrite it, without a CLOSE in between.
  • Error handling jumps back to the start of processing after a failure and repeats the OPEN.

How to find the problem

  1. DISPLAY the status after OPEN, or read the runtime message, which names the file.
  2. Put a DISPLAY before each OPEN and CLOSE of the file, or set breakpoints in a debugger, and trace the order they run in.
  3. In a subprogram, remember that WORKING-STORAGE keeps its values between calls unless the program is canceled or declared INITIAL. A first-time switch there survives, and so does an open file.

How to fix it

  • Keep an open flag for each file. Set it after a successful OPEN, clear it after CLOSE, and test it before opening.
  • CLOSE the file before opening it again in a different mode.
  • In subprograms, open files on the first call only, and give the caller a way to ask for the CLOSE at the end of the run.

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

Does file status 41 mean another job has the file open?

No. It means this program already has it open. A VSAM file held by another job gives 93 instead.

What happens to the file after a 41?

The failed OPEN doesn't change it. The file stays open in the mode the first OPEN used.

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.