Open In App

File Access Modes in COBOL

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

COBOL an acronym for Common Business Oriented Language is a computer programming language, which was designed for business use. In COBOL, file access modes are used to specify how to access data from various file organizations according to requirements. In COBOL, there are 3 types of file organization:

  • Sequential: Records that are stored and accessed in sequential order.
  • Indexed: Records that can be accessed both directly and sequentially.
  • Relative: Records are arranged according to their relative addresses.

Different file access modes can be used to access the data within the file for each file organization scheme. The following are the various access modes provided by COBOL:

  • Sequential Access
  • Random Access
  • Dynamic Access

The below table shows the file organizations and the access modes of files:

File organizationOrder of recordsAccess mode
SequentialOrder in which they were insertedSequential Access
Indexed  Based on the value of the record keySequential, Random, Dynamic Access
Relative  Order of relative record numberSequential, Random Dynamic Access

Sequential Access:

The file’s records can be accessed from top to bottom, or sequentially from the beginning of the file towards its end.

  • Records in sequential files can be retrieved in the order they were initially inserted.
  • For indexed files, the record key values are the parameters used to retrieve the records.
  • Relative record keys are used to retrieve records from relative files.

Cobol

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT GFGFILE1 ASSIGN TO DISK
    ORGANIZATION IS SEQUENTIAL
    ACCESS MODE IS SEQUENTIAL
  
  
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT GFGFILE1 ASSIGN TO DISK
    ORGANIZATION IS INDEXED
    ACCESS MODE IS SEQUENTIAL
    RECORD KEY IS GFG-KEY1
  
        
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT GFGFILE1 ASSIGN TO DISK
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS SEQUENTIAL
    RELATIVE KEY IS GFG-KEY1

                    

Random Access:

By providing the key value, records can be accessed randomly from any point in the file. Only Indexed and Relative files can be used in this mode.

  • For indexed files, records are retrieved in accordance with the key field value.
  • Records are retrieved for relative files in accordance with the value specified in the relative key.

Cobol

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT GFGFILE1 ASSIGN TO DISK
    ORGANIZATION IS INDEXED
    ACCESS MODE IS RANDOM
    RECORD KEY IS GFG-KEY1
  
        
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT GFGFILE1 ASSIGN TO DISK
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS SEQUENTIAL
    RELATIVE KEY IS GFG-KEY1

                    

Dynamic Access:

The records can be accessed both randomly and sequentially; for instance, we can use one file definition to obtain some records by providing the key values and some records by providing the key values in sequential order.

  • Records in sequential files can be retrieved by going from top to bottom.
  • Records can be obtained sequentially or randomly for relative and index files.

Cobol

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT GFGFILE1 ASSIGN TO DISK
    ORGANIZATION IS SEQUENTIAL
    ACCESS MODE IS DYNAMIC
    RECORD KEY IS GFG-KEY1
  
  
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT GFGFILE1 ASSIGN TO DISK
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS SEQUENTIAL
    RELATIVE KEY IS GFG-KEY1

                    


Next Article
Article Tags :

Similar Reads