Difference Between Sequential, Indexed, and Relative Files in COBOL
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.
Advantages of file handling:
- 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.
Disadvantages of file handling:
- It provides slow access.
- Cannot perform operations efficiently.
Entries for declaring a file:
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.
- 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.
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.
The Indexed file can be accessed sequentially same as Sequential file organization as well as randomly only if the RECORD-KEY is known.
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.
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.