File status 47: READ on a file not open for input
File status 47 means a READ was issued for a file that isn't open in INPUT or I-O mode. Either the OPEN failed, the file was opened OUTPUT or EXTEND, or it was already closed.
Other names for this code
Also written as FS 47, file status code 47, COBOL status 47, status key 47, READ file not open COBOL, SK47.
What it means
Status 47 is a logic error. READ needs the file to be open for input, either in INPUT mode or in I-O mode. When the file is closed, or open in OUTPUT or EXTEND mode, the READ fails with 47.
The rule is the same for QSAM, VSAM and line-sequential files. Under the older CMPR2 behavior 47 was reported as 92.
In practice 47 is nearly always a side effect. The OPEN before it failed, perhaps with 35 or 39, and the program didn't check the status, so the file never became open. The first READ then fails with 47, and that's the status people notice. Look for the earlier failure before changing anything around the READ.
Common causes
- The OPEN failed and its status wasn't checked.
- The file was opened OUTPUT or EXTEND, but the program also reads it.
- The file was closed, for example in an error routine, and a later paragraph reads it again.
- Another program in the run unit opened its own copy of the file, and this program's FD was never opened.
How to find the problem
- Find the OPEN for the file and DISPLAY its status. A failed OPEN is the most likely cause.
- Check the open mode on the OPEN statement. READ needs INPUT or I-O.
- Trace any CLOSE of the file that can run before the READ.
How to fix it
- Check the status after every OPEN and stop with a message naming the file if it fails.
- Open the file INPUT for reading only, or I-O if the program also has to update it.
- Keep an open flag per file and test it before reading, if the flow is complex.
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.
Related codes
Questions
Why do I get 47 when my program has OPEN INPUT?
The OPEN most likely failed. Display its status. A 35 or 39 there leaves the file closed, and the first READ then gets 47.
What is the difference between 47, 48 and 49?
They're the same kind of error for different statements. 47 is a READ, 48 is a WRITE and 49 is a REWRITE or DELETE on a file not open in a mode that allows it.