File Access Modes in COBOL

Introduction

  • Different access modes are used for each organization scheme.
  • These access modes are used to define accessing way of the file depending on the requirement of the program.
Types of file access modes are:

1. Sequential Access

When the access mode is sequential, the method of record retrieval changes according to the selected file organization.

  • For sequential file, records are accessed in the same order as they are inserted in the sequential file.
  • For indexed file, the parameter used to get the records using the record key value.
  • For relative files, Relative record keys are used to fetch the records.
Syntax:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS SEQUENCIAL
    ACCESS MODE IS SEQUENCIAL
    
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS INDEXED
    ACCESS MODE IS SEQUENCIAL
    RELATIVE KEY IS rec-key1
    ALTERNATE RECORD KEY IS rec-key2

    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
         FILE-CONTROL.
         SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENCIAL
RELATIVE KEY IS rec-key1

2. Random Access

In random access mode, the method of record retrieval is changed according to the selected file organization.

  • The random access mode is used for the INDEXED AND RELATIVE files only.
  • For the INDEXED file, records are accessed as the value placed in a key field. The key field is a primary key or alternate key.
  • For relative files, records are fetched through relative record keys.
Syntax:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS INDEXED
    ACCESS MODE IS RANDOM
    RELATIVE KEY IS rec-key1
    ALTERNATE RECORD KEY IS rec-key2

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS DYNAMIC
    RELATIVE KEY IS rec-key1

3. Dynamic Access

  • In dynamic access mode, both sequential and random access are supported in the same program.
  • This access mode is used for all types of files.
Syntax:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS SEQUENCIAL
    ACCESS MODE IS DYNAMIC
    RELATIVE KEY IS rec-key1
    ALTERNATE RECORD KEY IS rec-key2

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS DYNAMIC
    RELATIVE KEY IS rec-key1