Open In App

Difference Between Sequential, Indexed, and Relative Files in COBOL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Files are the collection of records related to a particular entity. Through file handling, we can store these records in an organized order. These records are stored either on magnetic tape or on a hard disk. The files are further classified into 3 types:

  1. Sequential file organization.
  2. Relative file organization.
  3. Indexed file organization.

Advantages of file handling:

  1. It has unlimited storage and thus stores a large volume of data.
  2. It stores the data permanently on the device.
  3. It reduces the re-editing of data.

Disadvantages of file handling:

  1. It provides slow access.
  2. Cannot perform operations efficiently.

Entries for declaring a file:

Python3




ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT RFILE ASSIGN TO Storage Device.
  [RESERVE INT-1{AREA/AREAS}]
  [ORGANIZATION IS {SEQUENTIAL/RELATIVE/INDEXED}]
  [ACCESS MODE IS {SEQUENTIAL/RANDOM/DYNAMIC}]
  [RELATIVE KEY Variable]
  [FILE STATUS IS DATA-NAME].
 DATA-DIVISION.
 FILE-SECTION.
 FD File Name.


The Sequential file organization stores the data in sequence order.  We can access the data sequentially and the data can be stored only at the end of the file. There are 2 types of sequential files:

  • A line sequential file is also known as a text file or ASCII file. It is a simple text file that can be edited by almost all PC editors. It separates each record from the other by adding a delimiter at the end of the record. In the case of windows and DOS, the carriage return (x”OD”) and line feed (x”OA”) is added at the end of the record whereas in UNIX only the line feed(x”OA”)is added at the end of the record.

Example:

Python3




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT FILE1 ASSIGN TO DISK
    ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILE1.
    01 STUDENT.
    02 RNO PIC 99.
    02 NAME PIC A(7).
    02 PERC PIC 99.99.


  • Record sequential file is default sequential file. These records are based either on the size(bytes) which is defined by the programmer or on the size of the record.
    • If the size of the record is defined by the programmer then it is known as Fixed length.
    • If the records are stored on the basis of the size of the records then it is known as Variable length.

Variable-length records save the space of the hard disk as compared to fixed-length records.

Example:

Python3




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT FILE1 ASSIGN TO DISK
  ORGANIZATION IS RECORD SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD LENGTH
  RECORDING MODE IS V
  RECORD CONTAINS 0 TO 99 CHARACTERS.
FD FILE1.
  01 STUDENT.
  02 RNO PIC 99.
  02 NAME PIC A(7).
  02 PERC PIC 99.99.


Indexed file organization stores the record sequentially depending on the value of the RECORD-KEY(generally in ascending order). A RECORD-KEY in an Indexed file is a variable that must be part of the record/data. In the case of Indexed files two types of files are created:

  1. Data file: It consists of the records in sequential order.
  2. Index file: It consists of the RECORD-KEY and the address of the RECORD-KEY in the data file.

The Indexed file can be accessed sequentially same as Sequential file organization as well as randomly only if the RECORD-KEY is known. 

Example:

Python3




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT IFILE ASSIGN TO DISK
  ORGANIZATION IS INDEXED
  ACCESS MODE RANDOM
  RECORD KEY RNO.
DATA DIVISION.
FILE SECTION.
FD IFILE.
  01 STUDENT.
  02 RNO PIC 99.
  02 NAME PIC A(7).
  02 PERC PIC 99.99.


Relative file organization stores the record on the basis of their relative address. Each record is identified by its Relative Record Number, a Relative Record Number is the position of the record from the beginning of the file. These records can be accessed sequentially same as Sequential file organization as well as randomly, to access files randomly the user must specify the relative record number.

Example:

Python3




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT RFILE ASSIGN TO DISK
  ORGANIZATION IS RELATIVE
  ACCESS MODE RANDOM
  RELATIVE KEY POS.
DATA DIVISION.
FILE SECTION.
FD RFILE.
  01 STUDENT.
  02 RNO PIC 99.
  02 NAME PIC A(7).
  02 PERC PIC 99.99.


Difference between Sequential, Indexed, Relative files:

Sequential files Indexed files Relative files
These files can be accessed only sequentially. These files can be accessed sequentially as well as randomly with the help of the record key. These files can be accessed sequentially as well as randomly with the help of their relative record number.
The records are stored sequentially. The records are stored based on the value of the RECORD-KEY which is the part of the data. The records are stored by their relative address.
Records cannot be deleted and can only be stored at the end of the file. It is possible to store the records in the middle of the file. The records can be inserted at any given position.
It occupies less space as the records are stored in continuous order. It occupies more space. It occupies more space.
It provides slow access, as in order to access any record all the previous records are to be accessed first. It also provides slow access(but is fast as compared to sequential access) as it takes time to search for the index. It provides fast access as provides the record key compared to the other two.
In Sequential file organization, the records are read and written in sequential order. In Indexed file organization, the records are written in sequential order but can be read in sequential as well as random order. In Relative file organization, the records can be written and read in sequential as well as random order.
There is no need to declare any KEY for storing and accessing the records. One or more KEYS can be created for storing and accessing the records. Only one unique KEY is declared for storing and accessing the records.


Last Updated : 07 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads