Skip to main content
eLearner.app
Module 5 · Lesson 1 of 29/10 in the course~15 min
Module lessons (1/2)

File Definition & File-Control

In COBOL, managing external files (such as text archives or binary records on disk) is one of the most important features for batch corporate applications. Since COBOL was designed long before modern databases, disk files act as the primary storage.

Connecting physical files stored on the operating system with the program's internal variables occurs in two phases:

  1. Logical-physical association in the ENVIRONMENT DIVISION.
  2. Definition of the data record in the DATA DIVISION.

1. Association: FILE-CONTROL and SELECT ASSIGN

In the chapter dedicated to program structure, we explored the ENVIRONMENT DIVISION. Within it, the INPUT-OUTPUT SECTION contains the FILE-CONTROL paragraph, which declares the external files used by the program.

To associate a logical file in the program (e.g., SALES-FILE) with a real physical file on disk (e.g., sales.dat), use the SELECT ... ASSIGN TO ... statement:

Code
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT SALES-FILE ASSIGN TO "sales.dat"
               ORGANIZATION IS LINE SEQUENTIAL.
  • SELECT SALES-FILE: Defines the logical name the program will use to reference the file.
  • ASSIGN TO "sales.dat": Specifies the actual physical file name (or path) on disk.
  • ORGANIZATION IS LINE SEQUENTIAL (optional): Indicates that the file is a plain text file where each line represents a record (separated by newline characters).

2. Declaration: FILE SECTION and FD

After associating the file in the ENVIRONMENT DIVISION, we must define the structure of its records in the DATA DIVISION. To do this, we use a dedicated section called FILE SECTION (which precedes the WORKING-STORAGE SECTION).

Every selected file must have an FD (File Description) entry that declares the record structure:

Code
       DATA DIVISION.
       FILE SECTION.
       FD SALES-FILE.
       01 SALES-RECORD.
           05 EMP-ID       PIC 9(5).
           05 EMP-NAME     PIC X(20).
           05 EMP-SALARY   PIC 9(6)V99.
  • FD SALES-FILE.: Identifies the file description for the logical name SALES-FILE. Note that it ends with a period.
  • 01 SALES-RECORD.: Specifies the group structure (record) associated with the file. Each read/write operation will exchange data using this memory structure.

Try it out

Exercise#cobol.m5.l1.e1
Attempts: 0Loading…

Complete the ENVIRONMENT DIVISION to declare the INPUT-OUTPUT SECTION and the FILE-CONTROL paragraph, associating the logical file SALES-FILE with the physical file 'sales.dat' using the SELECT ASSIGN statement.

Loading editor…
Show hint

Write ENVIRONMENT DIVISION. and INPUT-OUTPUT SECTION. in column 8 (7 spaces), then FILE-CONTROL. in column 8, and finally SELECT SALES-FILE ASSIGN TO 'sales.dat'. in column 12 (11 spaces).

Solution available after 3 attempts

Exercise#cobol.m5.l1.e2
Attempts: 0Loading…

Declare the DATA DIVISION and FILE SECTION for the program. Define the File Descriptor (FD) for SALES-FILE, associating it with a level 01 record named SALES-RECORD containing two subordinate fields: SALES-ID (4-digit numeric) and SALES-AMOUNT (5-digit numeric with 2 implicit decimals).

Loading editor…
Show hint

Use DATA DIVISION. and FILE SECTION. in column 8, FD SALES-FILE. in column 8, followed by 01 SALES-RECORD. in column 8, and the fields 05 SALES-ID PIC 9(4). and 05 SALES-AMOUNT PIC 9(5)V99. in column 12 on new lines.

Solution available after 3 attempts