File status 48: WRITE on a file not open for output
File status 48 means a WRITE was issued for a file that isn't open in a mode that allows writing. For sequential files that's OUTPUT or EXTEND. For indexed and relative files it depends on the access mode.
Other names for this code
Also written as FS 48, file status code 48, COBOL status 48, status key 48, WRITE file not open COBOL, SK48.
What it means
Status 48 is a logic error. WRITE needs the file to be open in a mode that allows adding records, and IBM's rules are stricter than many people expect.
For a sequential file, QSAM or line-sequential, WRITE is allowed in OUTPUT and EXTEND mode only. A QSAM file opened I-O lets you READ and REWRITE records in place, but not WRITE new ones. For an indexed or relative file with ACCESS MODE SEQUENTIAL, WRITE is allowed in OUTPUT and EXTEND. With random or dynamic access, it's allowed in OUTPUT and I-O.
If the file is closed, or open in a mode that doesn't allow WRITE, the statement fails with 48. Under the older CMPR2 behavior it was reported as 92. As with 47, the most common root cause is an OPEN that failed earlier without anyone checking.
Common causes
- The OPEN failed and its status wasn't checked, so the file was never open.
- The file was opened INPUT, but the program also adds records to it.
- A QSAM file was opened I-O to add records at the end. Adding records needs EXTEND.
- A KSDS with ACCESS MODE SEQUENTIAL was opened I-O and the program tries to add records. That combination needs DYNAMIC or RANDOM access.
- A trailer or summary record is written after the file was closed in an end-of-job routine.
How to find the problem
- DISPLAY the status of the OPEN for the file. A failed OPEN is the most likely cause.
- Check the open mode and the ACCESS MODE in the SELECT clause against the rules above.
- Trace any CLOSE of the file that can run before the WRITE.
How to fix it
- Check the status after every OPEN and stop if it fails.
- Use OPEN EXTEND to add records to the end of a sequential file.
- For a KSDS that the program both reads and adds to, use ACCESS MODE DYNAMIC and OPEN I-O.
- Write trailer records before the CLOSE.
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 can't I WRITE to a sequential file opened I-O?
For a sequential file, I-O mode is for reading records and rewriting them in place. New records go at the end, and that needs OPEN EXTEND.
What is the difference between 48 and 49?
48 is a WRITE on a file not open in a mode that allows writing. 49 is a REWRITE or DELETE on a file not open I-O.