COBOL File Handling

To store the data in COBOL in form of DISK or TAPE we use files.

Files are divided into records. Each record are divided into fields which contains the information about data. COBOL supports 3 types of files

  • Sequential File
  • Indexed File
  • Relative organization File

SEQUENTIAL FILE​

Let’s see some important characteristics of Sequential files-

  • We also call Sequential Files  as Flat file
  • The records are stored in a sequential manner one after the other.
  • To access the Nth record, we have to read first (N-1) records first.
  • New records are always added at the end of the file.
  • It can have a fix or variable length.
  • It is recommended to use the sequential file if the simple file read and write is required and there are the less frequent search of a random and dynamic accessing of record is required.

The Syntax of Sequential file

SELECT LOGICAL-FL ASSIGNED TO PHYSICAL FL
ORGANIZATION IS SEQUENTIAL.

This we code in the FILE-CONTROL under INPUT-OUTPUT SECTION which is defined under ENVIRONMENT DIVISION.

Sample COBOL Program to show how the file is defined in INPUT-OUTPUT Section

TutorialBrain-How Files are defiend in FILE-CONTROL.
TutorialBrain-How Files are defined in FILE-CONTROL.

The Layout and details of the file we declare here only. This must be declared in FILE-SECTION under FD clause. Lets master FD Clause-

FD stands for File Description which describes the layout of all the files which are used in the program. FD must be defined inside the FILE SECTION of the DATA DIVISION.

The Syntax of FD clause

DATA DIVISION.
FILE SECTION.
FD FILE-NAME.
[RECORD CONTAINS N characters]
[BLOCK CONTAINS I RECORDS]
[DATA RECORD IS RECORD-DET]
[RECORDING MODE IS {F/V/U}].
01 LOGICAL-FL-REC.
    05 VAR-1 PIC …
    05 VAR-2 PIC …
    ..
    ..

Here, the below sentences are optional
[RECORD CONTAINS N characters][BLOCK CONTAINS I RECORDS][DATA RECORD IS RECORD-DET][RECORDING MODE IS {F/V/U}].

Most used FD Format/Example 1

DATA DIVISION.
FILE SECTION.
FD FILE-NAME.
01 LOGICAL-FL-REC.
    05 VAR-1 PIC 9(2).
    05 VAR-2 PIC X(10).

Here the name LOGICAL-FL-REC depends from program to program. Based on the requirement, you can change this name. 

01 level record name can be any meaningful name and it can contain other sets of items with different picture clause as well. The above is just for example.

Sample COBOL Program to show how file structure is defined in FILE Section

TutorialBrain-File definition in FD clause in File-section
A TutorialBrain-File definition in FD clause in File-section

Let’s understand the various options which are under the FD clause.

RECORDS CONTAINS N CHARACTERS

‘RECORD CONTAINS’ describes the size of data Record.

Format 1:
RECORD CONTAINS 80 CHARACTERS
We can define like Format 1 for fixed length files

Format 2:
RECORD CONTAINS SIZE N-1 TO N-2 CHARACTERS
We can define like Format 2 for variable files.

Format 3:
RECORD IS VARYING IN SIZE N-1 TO N-2 [DEPENDING ON DATA-ITEM-NAME]
We can define like Format 3 Dynamic files.

DATA RECORD IS RECORD-DET

‘DATA RECORD’ provides the record details of the data name. Here RECORD-DET contains the layout of the file.

BLOCK CONTAINS I RECORDS

‘BLOCK CONTAINS’ we use to define the block size of the file. It decides the size of a physical record.

Format 1
BLOCK CONTAINS 80 CHARACTERS 
We can define like Format 1 for fixed length files

Format 2:
BLOCK CONTAINS N-1 TO N-2 CHARACTERS
We can define like Format 2 for variable files.

Format 3: 
BLOCK CONTAINS SIZE IN N-1 TO N-2 CHARACTERS DEPENDING ON DATA-ITEM-NAME 
We can define like Format 3 Dynamic files.

There is a special ‘BLOCK CONTAINS’ and the syntax for this is-
BLOCK CONTAINS 0 CHARACTERS

In some organizations, programmers code like this for sequential(QSAM) files. If we code this, the block size is determined at runtime.

RECORDING MODE IS {F/V/U}

‘RECORDING MODE’ we use this to describe the format of the logical records of the file.

Format 1
RECORDING MODE IS F 
This means that the logical record of the file is of a fixed length, hence all the records in the file will occupy a fixed length which is provided in a COBOL program or in JCL.

Format 2:
RECORDING MODE IS V
This means that the logical record of the file is of a variable length, hence all the records in the file will occupy a variable length as per the definition.

Format 3: 
RECORDING MODE IS U 
We use this when the file does not have fixed as well as variable record length and the records have unidentified record length.

There is a special sequential file ‘Line sequential file. Let’s understand about Line sequential file

LINE SEQUENTIAL FILE

  • Line sequential is a special type of Sequential file where each record is separated by a carriage return(X“0D”) or Line Feed (X“0A”) at the end of last non-space character.
  • This sequential files always contain variable-length records.
  • We also call as text files. For the Report file, we should define the file here.

The Syntax of Line Sequential file

SELECT LOGICAL-FL ASSIGNED TO PHYSICAL FL
ORGANIZATION IS LINE SEQUENTIAL.

This is compulsory to code  in the FILE-CONTROL under INPUT-OUTPUT SECTION which is defined under ENVIRONMENT DIVISION.

INDEXED FILE

Let’s see some important characteristics of INDEXED files-

  • Indexed files are files which we can access faster as compared to sequential files.
  • To access the INDEXED file, we use key-values.
  • The Indexed file uses the alphanumeric key as KEY.
  • We can access any record in any order using KEY in an INDEXED file.
  • The ACCESS MODE for the INDEXED file can be sequential as well as Random.
  • The Primary key in an INDEXED file must be unique.
  • ORGANIZATION IS INDEXED in case of Indexed files.
  • We cannot update the primary key in the INDEXED file.

Note:
If we need to write in an INDEXED file, we should write the records in increasing order of keys.

Example:
To write the record with key A123, we must write all the records with keyless than A123. Here the keys are also case sensitive so, A123 is not same as a123.
We cannot update the key field in the INDEXED file but we can delete a particular record. The record cannot be physically deleted only that particular memory location for the record will be made as inaccessible.

The Syntax of Indexed file Format 1

SELECT logical-fl ASSIGNED TO physical-fl
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS I-KEY
ALTERNATE KEY IS IA-KEY.

Here, It is compulsory to define I-KEY and AI-KEY in the FD clause in the FILE SECTION.

The Syntax of Indexed file Format 2

SELECT logical-fl ASSIGNED TO physical-fl
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS I-KEY
ALTERNATE KEY IS IA-KEY.

Here, It is compulsory to define I-KEY and AI-KEY in the FD clause in the FILE SECTION.

The Syntax of Indexed file Format 3

SELECT logical-fl ASSIGNED TO physical-fl
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS R-KEY.

Here, It is compulsory to define  R-KEY  FD clause in the FILE SECTION.

RELATIVE FILE

Let’s see some important characteristics of RELATIVE files-

  • We also call this  Relative Record Data Set(RRDS) file.
  • The records in Relative File we can access by using RRN (Relative Record Number).
  • It can have Random or Sequential access.
  • We can access the record in any order by declaring a ‘RECORD KEY’.
  • Here the key should be a numeric key only.

Suppose, we have a requirement to read a record with a particular key value which we already know, it is better to define the file as RRDS.

The Syntax of Relative file Format 1

SELECT logical-fl ASSIGNED TO physical-fl
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS R-KEY.

Here, It is compulsory to define R-KEY in the FD clause in the FILE SECTION.

The Syntax of Relative file Format 2

SELECT logical-fl ASSIGNED TO physical-fl
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RECORD KEY IS R-KEY.

Here, it is compulsory to define  R-KEY in the FD clause in the FILE SECTION.

Suppose, we have 9000 records in a file

SELECT logical-fl ASSIGNED TO physical-fl
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RECORD KEY IS R-KEY.

FD LOGICAL-FL.
01 LOGICAL-FL-REC.
05 R-KEY PIC 9(4).
05 R-NAME PIC X(20).
To hold 9000 records, we must define the key with PIC 9(4) which can hold a value from 0001 to 9999.

Tutorials for all brains!