Menu
CoddyTech

Shared Records

Part of the Programs That Work Together section of Coddy's COBOL Programming journey. Lesson 2 of 16.

A file's record layout is the one thing every program that reads the file must agree on. Put it in a copybook and copy it under the file's FD:

FD CUST-FILE.
    COPY CUSTREC.

The program that writes the file and every program that reads it copy the same layout. When a field grows, the copybook changes once and each program is compiled again.

A copybook can also hold only the 05 fields, so one layout serves two records. Each program writes its own 01 above the COPY:

01 CUST-IN.
    COPY CUSTFLDS.
01 BEST-CUST.
    COPY CUSTFLDS.

Both records now have a CUST-NAME, so the name alone is ambiguous. OF says which record a field belongs to:

DISPLAY CUST-NAME OF BEST-CUST
challenge icon

Challenge

Easy

The file CUSTIN uses the layout in CUSTREC.cpy. Copy it under the FD so the program lists each customer, starting with Ada Lovelace  1,250.50

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CUSTLIST.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT CUST-FILE ASSIGN TO CUSTIN
               ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       FILE SECTION.
       FD CUST-FILE.
           *> Copy the record layout here
       WORKING-STORAGE SECTION.
       01 EOF-FLAG       PIC X VALUE "N".
           88 END-OF-FILE  VALUE "Y".
       01 SHOW-BAL       PIC ZZ,ZZ9.99.
       PROCEDURE DIVISION.
           OPEN INPUT CUST-FILE
           PERFORM UNTIL END-OF-FILE
               READ CUST-FILE
                   AT END SET END-OF-FILE TO TRUE
                   NOT AT END
                       MOVE CUST-BALANCE TO SHOW-BAL
                       DISPLAY CUST-NAME " " SHOW-BAL
               END-READ
           END-PERFORM
           CLOSE CUST-FILE
           STOP RUN.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Programs That Work Together