File Access Modes
The file access modes specify how data is read from or written to files. The file access mode that we used, specifies how we interact the records within the file. Always choose the mode based on the application's requirements for efficiency and performance.
COBOL supports three access modes -
- Sequential access mode
- Random access mode
- Dynamic access mode
For instance, if we want to search for a record without knowing its exact position, an indexed or relative file with random or dynamic access might be more suitable. On the other hand, sequential access would be appropriate if we're processing every record in a file in order.
Sequential access mode -
Records are accessed one after another in the order they appear in the file. Sequential access mode applicable to -
- Sequential files -
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO DSName ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL
- Indexed files - No need to pass the rec-key or alt-key values. Reads from the first record in the file.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO DSName ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS rec-key ALTERNATE RECORD KEY IS alt-key
- Relative files - No need to pass the ws-rrn value and reads from the first record in the file.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO DSName ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL RELATIVE KEY IS ws-rrn
Random access mode -
Records are accessed based on a specific key value, allowing for the direct retrieval of any record without knowing its position or reading the preceding ones. Random access mode applicable to -
- Indexed files - Need to pass the rec-key or alt-key values. Reads from the record that matches with the rec-key or alt-key.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO DSName ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS rec-key ALTERNATE RECORD KEY IS alt-key
- Relative files - Need to pass the ws-rrn value. Reads from the record that matches with the ws-rrn.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO DSName ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS ws-rrn
Dynamic access mode -
A combination of both SEQUENTIAL and RANDOM access modes. The type of access (sequential or random) can be chosen during runtime based on the operations we perform. Dynamic access mode applicable to -
- Indexed files - Need to pass the rec-key or alt-key values to read the matched record dynamically. From that position, it starts reading sequentially.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO DSName ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS rec-key ALTERNATE RECORD KEY IS alt-key
- Relative files - Need to pass the ws-rrn value to read the matched record dynamically. From that position, it starts reading sequentially.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO DSName ORGANIZATION IS RELATIVE ACCESS MODE IS DYNAMIC RELATIVE KEY IS ws-rrn