Open In App

File Organization in COBOL

A record-based COBOL file is a collection of records and file organization deals with how records are stored on a backing disk storage unit. The way records are organized on the device is important because that affects how records can be accessed and the latency of accessing those records.

COBOL provides 3 different types of file organization:



Sequential File Organization:

The simplest method of organizing records on a disk. Records are organized one after another in a serialized manner. Irrespective of the type of storage device, these files are processed serially. Hence, accessing any record in this file requires accessing all the previous records. So we start reading from the very first read, and sequentially go through each record until the required record or the EOF.

Syntax:

INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name-jcl
ORGANIZATION IS SEQUENTIAL

Pros and cons of Sequential file organization:



Indexed Sequential File Organization:

Direct access of a record was not possible with Sequential organization, to overcome this Indexed Sequential file organization is used. The indexed file consists of two files – data file and index file. The data file is created just like a sequential file, but it can also be accessed randomly. Index file consists of the value of each key field and address of the corresponding record on the backing storage device. Reading a record from an indexed file does not require reading all previous records in the file, instead, the given key field is searched in the index file, and once found the stored address is accessed directly to read the corresponding record.

Syntax:

INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT file-name ASSIGN TO dd-name-jcl
  ORGANIZATION IS INDEXED
  RECORD KEY IS primary-key
  ALTERNATE RECORD KEY IS rec-key

Pros and cons of Indexed file organization:

Relative File Organization:

Relative file organization also allows direct/random access to stored records, but this file organization does not use an index. Instead, the key field itself is converted to an actual disk address, and hence there is no need for a search to find the record. The value of such a key field is called “relative record number”. Record in relative files is organized on ascending relative record numbers. Unlike indexed files, there can only be one key field and it has to be numeric. Also, file allocation happens for all the records starting from 1 to the record with the highest relative record number even though all the records are not yet populated.

A simple relative file organization may have a one-to-one correlation between key-value and the record’s disk location. For eg. a Student Info file is created with StudentID as a record key, whose value ranges from 1 to 999, then the record StudentID=1 can be placed on the first location on disk, StudentID=2 on the next disk location, and so on. To access a record with StudentID=458 means the system can directly go to disk location 458 and access the record. Similarly, there can be a different scheme that has a base value added to a relative record number, for eg. with a base value of 2000, the first record key value will be 2001 and the last would be 2999, given such a key value we would subtract 2000 from it to get the relative record number.

Syntax:

INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT file-name ASSIGN TO dd-name-jcl
  ORGANIZATION IS RELATIVE
  RELATIVE KEY IS rec-key

Pros and cons of Relative file organization:


Article Tags :