>>SOURCE FORMAT IS FREE *> cobol-copybook-copy-replacing.cob - ACCT-POST, the first of three *> programs sharing one record layout: read last month's *> account master, credit a month of interest to every open *> account with a balance, write this month's master *> Dialect: GnuCOBOL 3.x, free format (the copybook itself is laid out *> so that it is also valid in fixed format) *> Status : desk-checked against the GnuCOBOL Programmer's Guide (3.2 *> COPY, 10.1.4 Locating Copybooks, 7.6.7 ROUNDED) and the IBM *> Enterprise COBOL 6.4 Language Reference (pp. 688-697); *> compiled and run with GnuCOBOL 3.2 on 2026-10-29 *> Inputs : datasets/cobol-copybook-copy-replacing.dat (LINE SEQUENTIAL, *> LF line endings, 39 bytes per record, 18 records in *> account-id order) *> code/cobol-copybook-copy-replacing.cpy (the layout, brought *> in twice under two different prefixes) *> Outputs: acct-master-new.dat (same 39-byte layout) and DISPLAY lines *> Build : cobc -x -I code -o acct-post code/cobol-copybook-copy-replacing.cob *> (run from the folder that holds code/ and datasets/) *> Run : ./acct-post then ./acct-list and ./acct-audit IDENTIFICATION DIVISION. PROGRAM-ID. ACCT-POST. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT Old-Master ASSIGN TO "datasets/cobol-copybook-copy-replacing.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-Old-FS. SELECT New-Master ASSIGN TO "acct-master-new.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-New-FS. DATA DIVISION. FILE SECTION. *> One layout, two record areas. The LEADING phrase renames every *> word that begins with Acct-, so the old record's fields are Old-Id, *> Old-Balance and so on, and the new record's are New-Id, New-Balance. FD Old-Master. COPY "cobol-copybook-copy-replacing.cpy" REPLACING LEADING ==Acct-== BY ==Old-==. FD New-Master. COPY "cobol-copybook-copy-replacing.cpy" REPLACING LEADING ==Acct-== BY ==New-==. WORKING-STORAGE SECTION. 77 WS-Old-FS PIC XX. 77 WS-New-FS PIC XX. 01 WS-Flags. 05 WS-EOF-Flag PIC X VALUE "N". 88 End-Of-Old-Master VALUE "Y". 77 WS-Monthly-Rate PIC V9999 VALUE .0025. 77 WS-Interest PIC 9(7)V99 VALUE 0. 77 WS-Interest-Total PIC 9(7)V99 VALUE 0. 77 WS-Read PIC 9(5) VALUE 0. 77 WS-Written PIC 9(5) VALUE 0. 77 WS-Credited PIC 9(5) VALUE 0. 77 WS-Show-Count PIC ZZ,ZZ9. 77 WS-Show-Money PIC Z,ZZZ,ZZ9.99. PROCEDURE DIVISION. Main-Paragraph. OPEN INPUT Old-Master IF WS-Old-FS NOT = "00" DISPLAY "Cannot open the old master, status " WS-Old-FS STOP RUN END-IF OPEN OUTPUT New-Master IF WS-New-FS NOT = "00" DISPLAY "Cannot create acct-master-new.dat, status " WS-New-FS CLOSE Old-Master STOP RUN END-IF PERFORM Read-Old PERFORM UNTIL End-Of-Old-Master PERFORM Post-One-Account PERFORM Read-Old END-PERFORM CLOSE Old-Master New-Master MOVE WS-Read TO WS-Show-Count DISPLAY "ACCT-POST records read " WS-Show-Count MOVE WS-Written TO WS-Show-Count DISPLAY "ACCT-POST records written " WS-Show-Count MOVE WS-Credited TO WS-Show-Count DISPLAY "ACCT-POST accounts credited" WS-Show-Count MOVE WS-Interest-Total TO WS-Show-Money DISPLAY "ACCT-POST interest posted " WS-Show-Money STOP RUN. Read-Old. READ Old-Master AT END SET End-Of-Old-Master TO TRUE END-READ IF WS-Old-FS NOT = "00" AND WS-Old-FS NOT = "10" DISPLAY "Old master read failed, status " WS-Old-FS STOP RUN END-IF. *> Move the whole 39-byte record across, then change only the balance. Post-One-Account. ADD 1 TO WS-Read MOVE Old-Record TO New-Record IF Old-Open AND Old-Balance > 0 COMPUTE WS-Interest ROUNDED = Old-Balance * WS-Monthly-Rate ADD WS-Interest TO New-Balance ADD WS-Interest TO WS-Interest-Total ADD 1 TO WS-Credited END-IF WRITE New-Record IF WS-New-FS NOT = "00" DISPLAY "Write failed for account " Old-Id ", status " WS-New-FS CLOSE Old-Master New-Master STOP RUN END-IF ADD 1 TO WS-Written.