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:
- Logical-physical association in the
ENVIRONMENT DIVISION. - 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:
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:
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 nameSALES-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
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.
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
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).
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