COBOL Textbook

Part 3

Files and Records

Sequential, indexed, and relative files; copybooks; the record-layout discipline mainframe shops live by.

Part 3 is where COBOL meets the data it was built for: fixed-length records in files. Its four lessons cover the two file organisations most batch programs use, sequential and indexed, plus the copybook, the shared record layout that keeps every program in a shop reading the same bytes the same way.

Start with reading a sequential file to end of file: SELECT, FD, OPEN, the priming READ and AT END, on a 60-byte customer file that prints control totals. Writing a report file is the other half of a batch job, a paginated register with the run date in every page header, edited detail lines, a page total and a break every 23 lines. Indexed files load a parts master, then read by key, rewrite a record, browse a key range with START and READ NEXT, and delete. Copybooks bring one 39-byte account layout into three programs with plain COPY, REPLACING LEADING and REPLACE, and note where GnuCOBOL and IBM differ on search paths and nesting.

Every lesson in this part ships its data file alongside the listing, so a record layout can be checked byte by byte against the program that reads it. The Practice part assembles these pieces into complete batch jobs. Next in the queue for this part is SORT and MERGE, followed by relative files.

  1. 3.1

    COBOL sequential files: read a fixed-width file to end of file

    SELECT, FD, OPEN, the priming READ and AT END: a complete program reads a 60-byte customer file to end of file and prints control totals; data and copybook ship.

    6 min

  2. 3.2

    COBOL WRITE a report file: headers, detail lines, page breaks

    A GnuCOBOL program writes a paginated report: page header with the run date, column headings, edited detail lines, a page total, and a break every 23 lines.

    10 min

  3. 3.3

    COBOL indexed files: ORGANIZATION INDEXED, START, READ KEY, REWRITE

    A GnuCOBOL program loads a parts master into an indexed file, reads by key, rewrites a record, browses a key range with START and READ NEXT, and deletes; data ships.

    14 min

  4. 3.4

    COBOL copybooks: COPY, REPLACING, and one layout in three programs

    One 39-byte copybook in three GnuCOBOL programs: plain COPY, REPLACING LEADING, and REPLACE, with where GnuCOBOL and IBM differ on search paths and nesting.

    13 min