Difference Between Sequential, Indexed, and Relative Files in COBOL

07 Sep, 2021
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:
Sequential file organization.Relative file organization.Indexed file organization.
Sequential file organization.
Relative file organization.
Indexed file organization.
It has unlimited storage and thus stores a large volume of data.It stores the data permanently on the device.It reduces the re-editing of data.
It has unlimited storage and thus stores a large volume of data.
It stores the data permanently on the device.
It reduces the re-editing of data.
It provides slow access.Cannot perform operations efficiently.
It provides slow access.
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.
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:
Data file: It consists of the records in sequential order.Index file: It consists of the RECORD-KEY and the address of the RECORD-KEY in the data file.
Data file: It consists of the records in sequential order.
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:
Picked
COBOL
Difference Between
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.