MainframeMaster

COBOL Tutorial

COBOL File Description (FD)

Progress0 of 0 lessons

FILE SECTION and FD Entries

The FILE SECTION is a crucial part of the DATA DIVISION in COBOL programs that process files. Within the FILE SECTION, each file used by the program must have a File Description (FD) entry that defines the physical characteristics of the file and serves as a link between the physical file structure and the logical record layout.

Basic Structure

The FILE SECTION begins with the header "FILE SECTION." followed by FD entries for each file. Each FD entry corresponds to a file that was defined with a SELECT statement in the ENVIRONMENT DIVISION.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DATA DIVISION. FILE SECTION. FD EMPLOYEE-FILE LABEL RECORDS ARE STANDARD BLOCK CONTAINS 10 RECORDS RECORD CONTAINS 80 CHARACTERS. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC 9(5). 05 EMPLOYEE-NAME PIC X(30). 05 EMPLOYEE-ADDRESS PIC X(35). 05 EMPLOYEE-SALARY PIC 9(7)V99. FD PAYROLL-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 50 CHARACTERS. 01 PAYROLL-RECORD. 05 PR-EMPLOYEE-ID PIC 9(5). 05 PR-PAY-DATE PIC 9(8). 05 PR-GROSS-PAY PIC 9(7)V99. 05 PR-DEDUCTIONS PIC 9(7)V99. 05 PR-NET-PAY PIC 9(7)V99. 05 FILLER PIC X(6).

This example shows a FILE SECTION with two FD entries, one for an employee file and one for a payroll file. Each FD is followed by a record description (01-level item) that defines the logical record structure.

Relationship to ENVIRONMENT DIVISION

Each FD entry must correspond to a file that was previously defined with a SELECT statement in the ENVIRONMENT DIVISION. The file name used in the FD entry must match the file name used in the SELECT statement.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-FILE ASSIGN TO EMPFILE ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EMPLOYEE-ID FILE STATUS IS EMPLOYEE-FILE-STATUS. DATA DIVISION. FILE SECTION. FD EMPLOYEE-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC 9(5). 05 EMPLOYEE-NAME PIC X(30). 05 EMPLOYEE-ADDRESS PIC X(35). 05 EMPLOYEE-SALARY PIC 9(7)V99.

In this example, the file "EMPLOYEE-FILE" is defined in both the ENVIRONMENT DIVISION (with a SELECT statement) and the DATA DIVISION (with an FD entry). This connection links the physical file characteristics to the logical record layout.

Multiple Record Descriptions

An FD entry can be followed by multiple 01-level record descriptions, which is useful when a file contains different record types or when you need different views of the same data.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 100 CHARACTERS. 01 CUSTOMER-RECORD. 05 RECORD-TYPE PIC X. 88 CUSTOMER-HEADER VALUE "H". 88 CUSTOMER-DETAIL VALUE "D". 88 CUSTOMER-FOOTER VALUE "F". 05 CUSTOMER-DATA PIC X(99). 01 CUSTOMER-HEADER-RECORD REDEFINES CUSTOMER-RECORD. 05 HR-RECORD-TYPE PIC X. 05 HR-FILE-DATE PIC 9(8). 05 HR-RECORD-COUNT PIC 9(5). 05 HR-FILLER PIC X(86). 01 CUSTOMER-DETAIL-RECORD REDEFINES CUSTOMER-RECORD. 05 DR-RECORD-TYPE PIC X. 05 DR-CUSTOMER-ID PIC 9(5). 05 DR-CUSTOMER-NAME PIC X(30). 05 DR-CUSTOMER-ADDR PIC X(50). 05 DR-ACCOUNT-STATUS PIC X. 05 DR-FILLER PIC X(13). 01 CUSTOMER-FOOTER-RECORD REDEFINES CUSTOMER-RECORD. 05 FR-RECORD-TYPE PIC X. 05 FR-TOTAL-CUSTOMERS PIC 9(5). 05 FR-FILLER PIC X(94).

This example shows a file with three different record types (header, detail, and footer), each with its own record description. The REDEFINES clause indicates that each record layout is an alternative view of the same storage area.

LABEL RECORDS Clause

The LABEL RECORDS clause in an FD entry historically specified whether the file had standard labels, user labels, or no labels. In modern COBOL, this clause is often optional or treated as syntax-only, as file labeling is typically handled by the operating system or file system.

Syntax and Options

cobol
1
2
3
4
5
6
7
8
9
10
11
FD file-name LABEL RECORDS ARE STANDARD ... FD file-name LABEL RECORDS ARE OMITTED ... FD file-name LABEL RECORDS ARE data-name ...

  • STANDARD - The file has standard labels (most common)
  • OMITTED - The file does not have labels
  • data-name - The file has user labels, with the data-name identifying the user label records

Historical Context

The LABEL RECORDS clause dates back to the early days of COBOL when files were often stored on tapes or other sequential media. Labels provided information about the file, such as:

  • File name and creation date
  • Record format and blocking information
  • Expiration date for retention purposes
  • Security and access control information
  • Volume labels for multi-volume files

In modern systems, this information is typically managed by the file system or database management system, making the LABEL RECORDS clause less relevant. However, it is still required in some COBOL dialects for backward compatibility.

Modern Usage

In contemporary COBOL applications, you'll typically see one of these approaches:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
* Option 1: Include the clause with STANDARD FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD ... * Option 2: Omit the clause entirely (if your compiler allows) FD CUSTOMER-FILE ... * Option 3: Use a compiler directive to make it optional >>DEFINE LABEL-RECORDS-NOT-REQUIRED FD CUSTOMER-FILE ...

Most modern COBOL programs use Option 1 (LABEL RECORDS ARE STANDARD) for compatibility, even though the clause may not have a functional impact on the file processing.

BLOCK CONTAINS Clause

The BLOCK CONTAINS clause specifies the size of physical blocks or the blocking factor for a file. Blocking is the process of grouping multiple logical records into a single physical record to improve I/O efficiency. This clause is particularly important for files on tape or other sequential storage media.

Syntax and Options

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
* Specify number of records per block FD file-name BLOCK CONTAINS 10 RECORDS ... * Specify block size in characters or words FD file-name BLOCK CONTAINS 800 CHARACTERS ... FD file-name BLOCK CONTAINS 200 TO 400 CHARACTERS ... FD file-name BLOCK CONTAINS 100 WORDS ...

  • You can specify the block size in terms of RECORDS, CHARACTERS, or WORDS
  • For variable-length blocks, you can specify a range (TO clause)
  • If CHARACTERS or WORDS is omitted, CHARACTERS is assumed
  • A WORD is typically defined as a platform-specific unit (often 4 bytes)

Blocking Factor and Optimization

The blocking factor (number of records per block) affects I/O performance:

  • Larger blocks reduce the number of I/O operations needed to process a file
  • Smaller blocks use memory more efficiently and may improve random access
  • Optimal block size depends on the storage medium and access patterns
  • For sequential files, larger blocks usually improve performance
  • For indexed files, the block size affects the efficiency of index searches

Example with Performance Impact

cobol
1
2
3
4
5
6
7
8
9
* Scenario 1: Small blocking factor FD TRANSACTION-FILE-1 BLOCK CONTAINS 1 RECORD RECORD CONTAINS 100 CHARACTERS. * Scenario 2: Larger blocking factor FD TRANSACTION-FILE-2 BLOCK CONTAINS 50 RECORDS RECORD CONTAINS 100 CHARACTERS.

Impact of blocking on a file with 10,000 records of 100 characters each:

ScenarioBlock SizeBlocks RequiredI/O Operations
Small blocking100 bytes10,00010,000
Large blocking5,000 bytes200200

The second scenario requires significantly fewer I/O operations, potentially improving performance but requiring more memory for each I/O buffer.

Modern Considerations

In contemporary computing environments:

  • Many file systems and database systems automatically handle blocking
  • The BLOCK CONTAINS clause may be ignored by some runtime environments
  • For mainframe applications, proper blocking is still important for performance
  • For disk files, the operating system's buffer cache may mitigate some blocking issues
  • Cloud-based or distributed storage systems have their own optimization mechanisms

RECORD CONTAINS Clause

The RECORD CONTAINS clause specifies the size of logical records in a file. It defines how much data is processed as a single unit when reading from or writing to a file. This clause is essential for defining the record format, especially for fixed-length and variable-length record handling.

Syntax Variations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
* Fixed-length records FD CUSTOMER-FILE RECORD CONTAINS 100 CHARACTERS ... * Variable-length records FD TRANSACTION-FILE RECORD IS VARYING IN SIZE FROM 50 TO 150 CHARACTERS DEPENDING ON RECORD-SIZE ... * Omitting the clause (size determined from record description) FD PRODUCT-FILE ... 01 PRODUCT-RECORD. 05 PRODUCT-ID PIC 9(5). 05 PRODUCT-NAME PIC X(30). 05 PRODUCT-PRICE PIC 9(5)V99. * Record size is implicitly 42 characters

  • For fixed-length records, specify a single size
  • For variable-length records, specify a range using FROM and TO
  • Use DEPENDING ON to reference a field that contains the actual record length
  • If omitted, the record size is determined from the record description

Working with Variable-Length Records

When working with variable-length records, you must manage the record size field:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
IDENTIFICATION DIVISION. PROGRAM-ID. VARLEN. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT VARIABLE-FILE ASSIGN TO "VARDATA.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS FILE-STATUS. DATA DIVISION. FILE SECTION. FD VARIABLE-FILE RECORD IS VARYING IN SIZE FROM 10 TO 100 CHARACTERS DEPENDING ON RECORD-SIZE. 01 VARIABLE-RECORD. 05 RECORD-TYPE PIC X. 05 DATA-FIELD PIC X(99). WORKING-STORAGE SECTION. 01 FILE-STATUS PIC XX. 01 RECORD-SIZE PIC 9(3). PROCEDURE DIVISION. MAIN-PROCESS. OPEN OUTPUT VARIABLE-FILE * Write a short record MOVE "A" TO RECORD-TYPE MOVE "SHORT DATA" TO DATA-FIELD MOVE 10 TO RECORD-SIZE WRITE VARIABLE-RECORD * Write a longer record MOVE "B" TO RECORD-TYPE MOVE "THIS IS A MUCH LONGER DATA RECORD WITH MORE INFORMATION" TO DATA-FIELD MOVE 50 TO RECORD-SIZE WRITE VARIABLE-RECORD CLOSE VARIABLE-FILE STOP RUN.

This example shows how to write variable-length records. The RECORD-SIZE field in WORKING-STORAGE is set before each WRITE operation to indicate the actual length of the current record.

Fixed vs. Variable Length Considerations

AspectFixed-LengthVariable-Length
Storage EfficiencyLess efficient - all records use maximum spaceMore efficient - only uses needed space
Processing ComplexitySimpler to processMore complex - must track record sizes
Random AccessEasy - can calculate record positionDifficult - cannot predict record position
I/O PerformancePredictable, often fasterMay be slower due to additional processing
Use CaseUniform data, indexed filesDiverse data sizes, space optimization

CODE-SET Clause

The CODE-SET clause specifies the character code set used for a file. This is particularly important in environments where data might be exchanged between systems with different character encodings, such as EBCDIC (mainframes) and ASCII (most distributed systems).

Basic Syntax

cobol
1
2
3
4
5
6
7
8
FD DATA-FILE CODE-SET IS ALPHABET-NAME ... * Example with a specific alphabet FD IMPORT-FILE CODE-SET IS ASCII-ALPHABET ...

The ALPHABET-NAME must be defined in the SPECIAL-NAMES paragraph of the ENVIRONMENT DIVISION.

Complete Example with Character Set Conversion

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
IDENTIFICATION DIVISION. PROGRAM-ID. CODESET. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. ALPHABET ASCII-ALPHABET IS STANDARD-1 ALPHABET EBCDIC-ALPHABET IS STANDARD-2. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT ASCII-FILE ASSIGN TO "ASCIIDATA.TXT" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS ASCII-STATUS. SELECT EBCDIC-FILE ASSIGN TO "EBCDICDATA.DAT" ORGANIZATION IS SEQUENTIAL FILE STATUS IS EBCDIC-STATUS. DATA DIVISION. FILE SECTION. FD ASCII-FILE CODE-SET IS ASCII-ALPHABET RECORD CONTAINS 80 CHARACTERS. 01 ASCII-RECORD PIC X(80). FD EBCDIC-FILE CODE-SET IS EBCDIC-ALPHABET RECORD CONTAINS 80 CHARACTERS. 01 EBCDIC-RECORD PIC X(80). WORKING-STORAGE SECTION. 01 FILE-STATUSES. 05 ASCII-STATUS PIC XX. 05 EBCDIC-STATUS PIC XX. 01 EOF-FLAG PIC X VALUE 'N'. 88 END-OF-FILE VALUE 'Y'. PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM PROCESS-FILES UNTIL END-OF-FILE PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN INPUT ASCII-FILE OPEN OUTPUT EBCDIC-FILE IF ASCII-STATUS NOT = "00" DISPLAY "Error opening ASCII file: " ASCII-STATUS MOVE "Y" TO EOF-FLAG END-IF. PROCESS-FILES. READ ASCII-FILE AT END MOVE "Y" TO EOF-FLAG NOT AT END MOVE ASCII-RECORD TO EBCDIC-RECORD WRITE EBCDIC-RECORD END-READ. FINALIZATION. CLOSE ASCII-FILE CLOSE EBCDIC-FILE DISPLAY "File conversion completed.".

This example reads data from an ASCII file and writes it to an EBCDIC file. The CODE-SET clause ensures that character code conversion happens automatically during file operations. The ALPHABETs are defined in the SPECIAL-NAMES paragraph.

Character Set Considerations

Different computing environments use different character encodings:

EncodingCommon EnvironmentCharacteristics
EBCDICIBM mainframes, AS/4008-bit, non-ASCII compatible, multiple variants
ASCIIUNIX, Linux, Windows, Mac7-bit standard, widely used in text files
ANSI/WindowsWindows systems8-bit extension of ASCII with code pages
UTF-8Modern web, cross-platformVariable-length, Unicode compatible

The CODE-SET clause is crucial when exchanging data between these different environments, particularly in mainframe-to-distributed system integration scenarios.

Modern Usage and Alternatives

In contemporary COBOL environments:

  • Character set conversion is often handled by middleware or file transfer utilities
  • Unicode support varies across COBOL implementations
  • Some environments offer built-in functions for character set conversion
  • Enterprise integration tools often handle encoding issues
  • For complex character set issues, specialized conversion routines may be needed

Exercises

Exercise 1: Creating FD Entries

Write an FD entry for a customer master file with the following requirements:

  • Fixed-length records of 200 characters
  • Blocked with 20 records per block
  • Standard labels
  • Customer ID (9 digits), name (50 characters), address (100 characters), and status code (1 character)

Solution
cobol
1
2
3
4
5
6
7
8
9
10
FD CUSTOMER-MASTER-FILE LABEL RECORDS ARE STANDARD BLOCK CONTAINS 20 RECORDS RECORD CONTAINS 200 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(9). 05 CUSTOMER-NAME PIC X(50). 05 CUSTOMER-ADDRESS PIC X(100). 05 CUSTOMER-STATUS PIC X. 05 FILLER PIC X(40).

Note that a FILLER field is added to pad the record to 200 characters.

Exercise 2: Variable-Length Records

Write the FILE SECTION for a program that processes transaction records with variable lengths ranging from 50 to 200 characters. The record length is stored in a field called TRANS-LENGTH. Include appropriate record descriptions.

Solution
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
FILE SECTION. FD TRANSACTION-FILE LABEL RECORDS ARE STANDARD RECORD IS VARYING IN SIZE FROM 50 TO 200 CHARACTERS DEPENDING ON TRANS-LENGTH. 01 TRANSACTION-RECORD. 05 TRANS-CODE PIC X(2). 05 TRANS-DATE PIC 9(8). 05 TRANS-AMOUNT PIC 9(7)V99. 05 TRANS-DESCRIPTION PIC X(181). WORKING-STORAGE SECTION. 01 TRANS-LENGTH PIC 9(3).

The TRANS-LENGTH field in WORKING-STORAGE will hold the actual length of each record and must be set before each WRITE operation.

Exercise 3: Multiple Record Types

Create a FILE SECTION for an inventory file that contains three types of records: header records (type 'H'), detail records (type 'D'), and trailer records (type 'T'). Each record is 100 characters long.

Solution
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
FILE SECTION. FD INVENTORY-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 100 CHARACTERS. 01 INVENTORY-RECORD. 05 RECORD-TYPE PIC X. 88 HEADER-RECORD VALUE 'H'. 88 DETAIL-RECORD VALUE 'D'. 88 TRAILER-RECORD VALUE 'T'. 05 RECORD-DATA PIC X(99). 01 HEADER-RECORD REDEFINES INVENTORY-RECORD. 05 HR-TYPE PIC X. 05 HR-FILE-DATE PIC 9(8). 05 HR-COMPANY-NAME PIC X(50). 05 HR-VERSION PIC 9(2). 05 HR-FILLER PIC X(39). 01 DETAIL-RECORD REDEFINES INVENTORY-RECORD. 05 DR-TYPE PIC X. 05 DR-ITEM-NUMBER PIC 9(10). 05 DR-ITEM-NAME PIC X(30). 05 DR-QUANTITY PIC 9(5). 05 DR-UNIT-PRICE PIC 9(5)V99. 05 DR-LOCATION PIC X(10). 05 DR-STATUS PIC X. 05 DR-FILLER PIC X(36). 01 TRAILER-RECORD REDEFINES INVENTORY-RECORD. 05 TR-TYPE PIC X. 05 TR-RECORD-COUNT PIC 9(10). 05 TR-TOTAL-ITEMS PIC 9(10). 05 TR-TOTAL-VALUE PIC 9(10)V99. 05 TR-FILLER PIC X(66).

This solution uses REDEFINES to provide different views of the same record area depending on the record type. Condition names (88-level items) make it easy to check record types in the procedure division.

Test Your Knowledge

1. What is the purpose of the FD entry in COBOL?

  • To establish a connection between COBOL programs and external files
  • To describe the physical characteristics of a file
  • To define file organization in the ENVIRONMENT DIVISION
  • To create a new file system

2. Where in a COBOL program would you find the FD entry?

  • ENVIRONMENT DIVISION
  • IDENTIFICATION DIVISION
  • FILE SECTION of the DATA DIVISION
  • PROCEDURE DIVISION

3. What is the purpose of the LABEL RECORDS clause in an FD entry?

  • To create backup labels for files
  • To specify whether records contain user labels
  • To apply encryption labels
  • To define named constants

4. What does the BLOCK CONTAINS clause specify?

  • The number of logical records in each physical block
  • The size of individual fields in the record
  • The number of files that can be processed simultaneously
  • The maximum record size

5. What is the relationship between a file defined with SELECT in the ENVIRONMENT DIVISION and its FD entry?

  • They must use different file names
  • The file name must be the same in both the SELECT and FD entry
  • The SELECT statement must come after the FD entry
  • There is no relationship between them

Frequently Asked Questions