FILE ALLOCATION is a key concept in COBOL programming. A file is a collection of data related to a set of entities and typically exists on a magnetic tape or a disk. We refer file as PS in the Mainframe environment. In File, data is organized as records. Each record is divided into fields that contain information about data.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT [OPIONAL] file-name ASSIGN TO DDNAME [ORGANIZATION IS {SEQUENTIAL/INDEXED/RELATIVE}] [ACCESS IS {SEQUENTIAL/RANDOM/DYNAMIC}] [RECORD KEY IS key-name] [RELATIVE KEY is rel-key] [ALTERNATE RECORD KEY IS alt-key {WITH/WITHOUT} DUPLICATES] [FILE STATUS IS file-status] [RESERVE number AREA] DATA-DIVISION. FILE-SECTION. FD File Name.
JCL: //INPUTFL DD DSN=DEPT1.EMP1.DATA1,DISP=SHR COBOL: SELECT INFILE ASSIGN TO UT-S-INPUTFL.
COBOL supports 3 types of files
Let’s see some important characteristics of Sequential files-
Syntax:
SELECT LOGICAL-FL ASSIGNED TO PHYSICAL FL
Define this in FILE-CONTROL under INPUT-OUTPUT SECTION which is defined under ENVIRONMENT DIVISION. The Layout and details of the file must be declared in FILE-SECTION under FD clause.
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-INPUT-REC. 05 INPT-VAR-1 PIC 9(9). 05 INPT-VAR-2 PIC X(9). ………… …………
It describes the size of the 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 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.
It provides the record details of the data name. Here RECORD-DET contains the layout of the file.
It is used 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 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.
It is used 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
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.
Let’s see some important characteristics of INDEXED files-
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.
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.
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.
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.
Let’s see some important characteristics of RELATIVE files-
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.
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.
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. |
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…