COBOL Sequential and Relative File

Sequential file handling in COBOL

  • Sequential file is also known as a flat-file.
  • It stores records in a sequential manner, i.e., one after another, stacked.
  • In this, we cannot directly access to the Nth record. For this, we have to read the records till N-1th before proceeding to the Nth record.
  • All the new records are added to the end, i.e., first come, first serve.
  • These files can have either fixed or variable length.
  • If there is a case of simple read and write a function and very little search of the record is required, these files are recommended.

SYNTAX =>

These must be coded in the FILE – CONTROL SECTION under INPUT-OUTPUT SECTION, defined under ENVIRONMENTAL DIVISION.

SAMPLE PROGRAM -1

This is a demo program to learn and understand the concept of sequential file handling and how to assign values from a different subprogram.

THE OUTPUT WILL BE

Here in this program, we have assigned a few variables with different values and later manipulated the output using different data file handling properties to clarify the different concepts related to CALLED / SUBPROGRAM AND MAIN / CALLING PROGRAM.

SAMPLE PROGRAM -2

This is a demo program to learn and understand the concept of sequential file handling and how to read data from input files.

THE OUTPUT WILL BE

Relative File Handling in COBOL

  • The relative file is also known as relative record data set or RRDS-file.
  • The record in these files can be accessed by – RRN (Relative Record Number)
  • It contains both random and sequential access.
  • The record can be accessed in any order by declaring a ‘RECORD KEY‘. The key should be a numeric constant.

SYNTAX =>

Example:

Suppose we have a data of 9000 file record

In order to hold 1000 records, we must define the key with PIC clause 9 (4) that can hold a value from 0001 to 9999.

source