READ and WRITE
The COBOL statements that open, read, write, update and close files.
Also written OPEN, CLOSE, REWRITE, AT END, INVALID KEY, WRITE FROM
A COBOL program works with a file in a fixed pattern: open it, read or write records, then close it.
OPEN names how the file will be used. INPUT reads, OUTPUT creates new records, I-O reads and updates, and EXTEND adds records to the end. READ gets the next record into the record area, and AT END runs code when there are no more: READ ACCT-FILE AT END SET WS-EOF TO TRUE END-READ. For an indexed file, a READ can ask for a record by key, with INVALID KEY for when no record matches.
WRITE adds a record, often with FROM to copy it from a working-storage area first. REWRITE replaces the record most recently read, for updating in place. DELETE removes a record from an indexed file. CLOSE finishes with the file.
The classic batch loop reads the first record, then performs a processing paragraph until end of file, reading the next record at the end of each pass. Check the FILE STATUS after every operation. Most file problems, such as a missing DD statement or a wrong record length, show up first as a status that is not 00.
Where can I learn this hands-on?
Right here, at ZedBank. You take a junior job at a simulated bank and learn each part of the work by doing it, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- File descriptionThe FD entry in a COBOL program that describes a file's records, connecting the program to data supplied by the JCL.
- FILE STATUSA two-character code that COBOL sets after every file operation, telling the program whether it worked and, if not, why.
- KSDSThe most common VSAM dataset type, where every record has a unique key used to find it directly.
- Sequential datasetThe simplest kind of dataset: records stored one after another and normally read from start to finish.
- PERFORMThe COBOL statement that runs a paragraph and returns, and the way loops are written.
- DD statementThe JCL statement that connects a name inside a program to a real dataset outside it.