Menu
CoddyTech

SELECT and FD

Part of the Files and Reports section of Coddy's COBOL Programming journey. Lesson 1 of 22.

Business data lives in data sets: files of records, such as a shop's orders. Each line of the data set ORDIN is one record:

A100Tea     005
A101Coffee  012
A102Jam     001

A program names each file it uses in a SELECT, in the FILE-CONTROL paragraph of the ENVIRONMENT DIVISION. ASSIGN TO ties that name to a data set:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT ORDER-FILE ASSIGN TO ORDIN
        ORGANIZATION IS LINE SEQUENTIAL.

LINE SEQUENTIAL says each record is one line of text, the way the data sets in these lessons are stored. A mainframe data set has no line ends: its records follow one another, often all the same length.

The FD, in the FILE SECTION of the DATA DIVISION, describes the record. A record has no separators: the pictures say where each field starts.

FILE SECTION.
FD ORDER-FILE.
01 ORDER-REC.
    05 ORD-ID PIC X(4).
    05 ORD-ITEM PIC X(8).
    05 ORD-QTY PIC 9(3).

The FILE SECTION comes before the WORKING-STORAGE SECTION.

challenge icon

Challenge

Easy

The program reads the first customer of CUSTIN, but its SELECT is missing. Write it in FILE-CONTROL: the file CUST-FILE, assigned to CUSTIN, line sequential. The program prints C001 Ann Lee.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SELFD.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           *> Write the SELECT for CUST-FILE here
       DATA DIVISION.
       FILE SECTION.
       FD CUST-FILE.
       01 CUST-REC.
           05 CUST-ID PIC X(4).
           05 CUST-NAME PIC X(10).
           05 CUST-CITY PIC X(6).
           05 CUST-BAL PIC 9(5).
       PROCEDURE DIVISION.
           OPEN INPUT CUST-FILE.
           READ CUST-FILE.
           DISPLAY CUST-ID " " CUST-NAME.
           CLOSE CUST-FILE.
           STOP RUN.

All lessons in Files and Reports