>>SOURCE FORMAT IS FREE *> cobol-read-sequential-file.cob — read a fixed-width customer file *> to end of file and print control totals *> Dialect: GnuCOBOL 3.x, free format *> Status : desk-checked against the GnuCOBOL 3.2 Programmer's Guide; *> NOT compiled (no cobc on the authoring machine) *> Inputs : datasets/cobol-read-sequential-file.dat (LINE SEQUENTIAL, *> LF line endings, 60 bytes per record, 12 records) *> code/cobol-read-sequential-file.cpy (the record layout) *> Outputs: one DISPLAY line per record plus four control-total lines *> Build : cobc -x -I code -o read-customers code/cobol-read-sequential-file.cob *> (run from the folder that holds code/ and datasets/) *> Run : ./read-customers (Windows: read-customers.exe) IDENTIFICATION DIVISION. PROGRAM-ID. READ-CUSTOMERS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT Customer-File ASSIGN TO "datasets/cobol-read-sequential-file.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-File-Status. DATA DIVISION. FILE SECTION. FD Customer-File. COPY "cobol-read-sequential-file.cpy". WORKING-STORAGE SECTION. 77 WS-File-Status PIC XX. 01 WS-Flags. 05 WS-EOF-Flag PIC X VALUE "N". 88 End-Of-File VALUE "Y". 77 WS-Record-Count PIC 9(5) VALUE 0. 77 WS-Debit-Total PIC 9(9)V99 VALUE 0. 77 WS-Credit-Total PIC 9(9)V99 VALUE 0. 77 WS-Net PIC S9(9)V99 VALUE 0. 77 WS-Show-Amount PIC Z(6)9.99. 77 WS-Show-Total PIC Z(8)9.99. 77 WS-Show-Net PIC -(9)9.99. 77 WS-Show-Count PIC ZZZZ9. PROCEDURE DIVISION. Main-Paragraph. OPEN INPUT Customer-File IF WS-File-Status NOT = "00" DISPLAY "Open failed, file status " WS-File-Status STOP RUN END-IF *> Priming read, then loop while the last read succeeded. PERFORM Read-Next-Customer PERFORM UNTIL End-Of-File PERFORM Process-Customer PERFORM Read-Next-Customer END-PERFORM CLOSE Customer-File PERFORM Show-Totals STOP RUN. Read-Next-Customer. READ Customer-File AT END SET End-Of-File TO TRUE NOT AT END ADD 1 TO WS-Record-Count END-READ IF WS-File-Status NOT = "00" AND WS-File-Status NOT = "10" DISPLAY "Read failed, file status " WS-File-Status STOP RUN END-IF. Process-Customer. MOVE Cust-Balance TO WS-Show-Amount DISPLAY Cust-Id " " Cust-Name " " Cust-City " " WS-Show-Amount " " Cust-DC EVALUATE Cust-DC WHEN "D" ADD Cust-Balance TO WS-Debit-Total WHEN "C" ADD Cust-Balance TO WS-Credit-Total WHEN OTHER DISPLAY "Unexpected flag [" Cust-DC "] on " Cust-Id END-EVALUATE. Show-Totals. MOVE WS-Record-Count TO WS-Show-Count DISPLAY "Records read : " WS-Show-Count MOVE WS-Debit-Total TO WS-Show-Total DISPLAY "Debit total : " WS-Show-Total MOVE WS-Credit-Total TO WS-Show-Total DISPLAY "Credit total : " WS-Show-Total COMPUTE WS-Net = WS-Debit-Total - WS-Credit-Total MOVE WS-Net TO WS-Show-Net DISPLAY "Net owed : " WS-Show-Net.