跳转到主要内容
eLearner.app
模块 5 · 第 1 课(共 2)课程中的9/10~15 min
模块课程(1/2)

文件定义和文件控制

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

锻炼#cobol.m5.l1.e1
尝试:0加载中...

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.

正在加载编辑器...
显示提示

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).

3 次尝试后可用的解决方案

锻炼#cobol.m5.l1.e2
尝试:0加载中...

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).

正在加载编辑器...
显示提示

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.

3 次尝试后可用的解决方案