FILE SECTION
The FILE SECTION is a subsection of the DATA DIVISION. The record structure of the files should be declared in the FILE SECTION, and it contains all logical structures of the files that are used in the program. When we work with files (input files, output files, or both), the FILE SECTION helps us select the format these records should follow.
Syntax -
File-description-entries -
File-description-entry supplies information about the file's record name, record length, block length, key, recording mode, physical structure, etc.
Detailed Syntax -
For Sequential file -
For Relative|Indexed file -
For Sort|Merge file -
Parameters -
FD|SD logical-file-name -
- FD is a File Description level indicator that declares the record structure of the file.
- SD is the Sort Description level indicator that declares the record structure of the sort file.
- FD entry is coded for all physical files except sort or merge files, and SD entry is coded for each sort or merge work file.
- One FD entry is coded for one file.
- FD|SD entries coding should start in AREA-A, and the remaining entries coding should start in AREA-B.
- Logical-file-name should be the same as the file name associated with the SELECT clause and should be unique within the program.
RECORD CONTAINS .. -
The RECORD CONTAINS is used to specify the record size in terms of characters. If it is ignored, the compiler calculates the record lengths from the record layout.
BLOCK CONTAINS .. -
The BLOCK CONTAINS clause is used to specify the block size for the file in terms of characters or records per block. The CHARACTERS phrase has the integer value that reflects the block size of the record. The BLOCK CONTAINS clause can be ignored when the records in the associated file are not blocked, or the associated file is a VSAM file.
- CHARACTERS - It specifies the number of bytes required to store the physical record. The CHARACTERS phrase is the default.
- RECORDS - If the RECORDS option is used, it specifies the number of logical records included in BLOCK.
- BLOCK CONTAINS clause always depends on the RECORD CONTAINS clause.
- The BLOCK CONTAINS clause is syntax-checked but does not affect the program's execution when specified under an SD.
- The BLOCK CONTAINS clause cannot be used with the RECORDING MODE U clause.
LABEL RECORDS -
The LABEL RECORDS specifies the existence or absence of labels. It is the only syntax checked but does not affect the program's execution.
- STANDARD - If STANDARD coded, it specifies labels exist for this file. STANDARD is coded for mass storage devices and tape devices.
- OMITTED - If OMITTED is coded, it specifies that no labels exist for the file. OMITTED is allowed for tape devices.
DATA RECORDS -
The DATA RECORD links the record layout with the file. It is syntax checked, and it does not impact file operations but links the record layout to the file.
RECORDING MODE -
The RECORDING MODE specifies how logical records are included and how the logical records are read/written to the file. It is optional and only required for PS files. It is ignored and unnecessary for VSAM files and does not apply to sorting or merging files.
The valid recording modes are -
- F (fixed) - It specifies the file only contains fixed-length records, and each block has a fixed number of records.
- V (variable) - It specifies the file can contain variable-length records. A block can have more than one record.
- U (fixed or variable) - It specifies the file contains either fixed-length or variable-length records depending on runtime value. One block has only one record.
- S (spanned) - It specifies the file contains either fixed-length or variable-length records that can be larger than a block. The record is distributed on more than one block.
Different Record Lengths -
There are three types of declarations based on the record lengths -
- Fixed length records - Specifies the file has fixed-length records and declaration is
Declaration for the fixed-length record of length 80 -RECORD CONTAINS record-length CHARACTERS BLOCK CONTAINS block-length CHARACTERS
RECORD CONTAINS 80 CHARACTERS BLOCK CONTAINS 800 CHARACTERS
- Variable length records - Specifies the file has variable-length records and declaration is
Declaration for the variable-length record of length 80 to 100 -RECORD CONTAINS min-rec-length TO max-rec-length CHARACTERS BLOCK CONTAINS min-blk-length TO max-blk-length CHARACTERS
RECORD CONTAINS 80 TO 100 CHARACTERS BLOCK CONTAINS 800 TO 1000 CHARACTERS
- Dynamic length records - Specifies the file has variable-length records but the record length will be passed at runtime.
The declaration is
Declaration for the Dynamic length record of length 80 t0 100 -RECORD IS VARYING IN SIZE FROM min-length TO max-length CHARACTERS DEPENDING ON variable1 BLOCK CONTAINS min-blk-length TO max-blk-length CHARACTERS
RECORD CONTAINS 80 TO 100 CHARACTERS DEPENDING ON ws-length BLOCK CONTAINS 800 TO 1000 CHARACTERS
For example - Declaring a file with length 80.
----+----1----+----2----+----3----+----4----+
DATA DIVISION.
FILE SECTION.
FD file1
RECORD CONTAINS 80 CHARACTERS
BLOCK CONTAINS 800 CHARACTERS
RECORDING MODE IS F
DATA RECORD IS emp-rec.
Record-description-entry -
Record-description-entry specifies the record structure of the file, and it should be defined as an alphanumeric group variable or an elementary variable.
For example - Declaring a record description entry length 80.
----+----1----+----2----+----3----+----4----+
01 STD-REC PIC X(80).
Practical Example -
Scenario - Showing the file declaration in the COBOL program.