From the viewpoint of a COBOL application program, a file is a named collection of data records that is provided with a logical structure (file organization), has specific record formats, and is stored on one or more data storage media, which are referred to as volumes.
COBOL programs access files by making use of functions provided by the Data Management System (DMS). The particular DMS access method used for this purpose is determined by the file organization.
As seen from the perspective of DMS, the accessing of a file always represents a transfer of data blocks between a peripheral storage device and a part of the main memory, called the buffer, which is an area set up by the application program for accommodating the data blocks.
File organization and DMS access methods
The logical structure of a file, and thus the method by which it is accessed, is defined by its type of organization. The file organization is specified at file creation and cannot be changed subsequently.
In COBOL, files may be organized as sequential, relative, or indexed files. The features and characteristics of the various types of file organization are detailed in section "Sequential file organization", section "Relative file organization" and section "Indexed file organization". Each of the above organization types corresponds to an access method of the DMS.
The relationship is shown in the following table:
File organization | DMS access method |
sequential | SAM |
relative | ISAM/UPAM |
indexed | ISAM |
Table 16: File organization and DMS access methods
Data records and record formats
A (logical) data record represents the unit of a file that can be accessed by a COBOL program with a single I-O statement. Each read operation makes a record available to the program, while each write statement creates a record in the file.
The records of a file may be classified according to their record format. Depending on the type of file organization, the following record formats are permitted in COBOL:
Fixed-length records (RECFORM=F)
All records of a file are of the same length and contain no information regarding the record length.
Variable-length records (RECFORM=V)
The records of a file can have different lengths; each record contains a specification of its length in the first word, called the record length field.
In a COBOL program, this record length field does not form a part of the record description entry. This means that its content cannot be explicitly accessed unless a RECORD clause with the DEPENDING ON phrase is specified for the file (see “COBOL2000 Reference Manual” [1]).Records of undefined length (RECFORM=U)
The records of a file may vary in length but include no record length information.
Data blocks and buffers
A (logical) data block is the unit of a file that is transferred (by the DMS) between peripheral storage and main memory during a file access operation. In order to such data blocks, the program reserves a storage area in its address space. This reserved area is called the buffer.
A logical block may consist of one or more records; however, a record must not be distributed over more than one logical block.
If a logical block contains more than one data record, these records are said to be blocked. Only records of fixed or variable length can be blocked. Blocking is not possible with records of undefined length.
In terms of size, a logical block (and thus a buffer) may be defined:
for disk files, as a standard block, i.e. a physical block (PAM block) of 2048 bytes or integral multiples thereof (up to 16 PAM blocks) and
for magnetic tape files, additionally as a non-standard block of any length up to 32767 bytes.
To simply adaptations to future disk formats, only even-numbered multiples of 2048 bytes should be used as the block size in the ADD-FILE-LINK command or in the program specifications.
During compilation, the compiler calculates a value for the buffer size for each file on the basis of record and block length specifications given in the compilation unit. This default value can be modified during the assignment of the file by specifying the BUFFER-LENGTH operand in the ADD-FILE-LINK command. It must be noted, however, that
the buffer must be at least as large as the longest data record, and
there must be space for the management information (PAM key) in the buffer when processing in non-key format (BLKCTRL = DATA) (see section "Disk and file formats").
Except in the case of newly created files (OPEN OUTPUT), the block size entered in the catalog always takes priority over block size specifications in the program or ADD-FILE-LINK command.