Recently I conducted some interviews. I asked a few questions many mainframe programmers on the below file handling concepts in COBOL.
Many people confused to tell answers. But, these are very basic questions, everyone has to remember always.
After years of programming, we are forgetting, these things…
Really you need the below points in your mind always as long as you are working in COBOL development projects.
Note: Everyone knows about handling of sequential files. But, people confused about Relative files and Indexed files.
Access mode is Random
The organization is Relative ==> Why we use this is to say that the file is relative. No problem we can give “Indexed” also.
To access relative files, you need to give the above information in the SELECT statement of COBOL.
OPEN INPUT File1 Read File1 Key is xyz Close File
You can read the relative files in the above passion. Each time you need to supply KEY value.
OPEN INPUT File1 Access mode is Random Organization is Indexed
To access Indexed files, you need to give the above information in the SELECT statement of COBOL.
♣ Access Mode is Random Vs Access Mode is Dynamic:
- The access mode is Random means you can retrieve a record matching to a KEY value.
- Whereas Access mode is Dynamic- We can establish a position first dynamically, and we can read records sequentially. START Verb we use in Dynamic access mode.
The ORGANIZATION IS RELATIVE clause is used to specify that this is a relative file.
- The ACCESS MODE clause is optional. If this clause is omitted, the default mode is SEQUENTIAL.
- A relative file with ACCESS MODE SEQUENTIAL operates logically very much like an ORGANIZATION IS SEQUENTIAL file, except that you can delete records (see DELETE below) and position to a given location within the file (see START below).
If ACCESS MODE IS RANDOM, the target record for every I-O statement is specified by the value of data-name-1.
ACCESS MODE IS DYNAMIC is the most flexible. By using specific syntax in the I-O statements, you can process the file sequentially or randomly at any time.
The downside is that you must specify which type of access you want for each read operation. See the individual I-O statements that follow for more details.
The data-name-1 must be an integer numeric item, and its PICTURE cannot contain the P symbol. Obviously, it must be large enough to contain the highest record number you will need.
It also must not be contained in any of the 01 records under the FD for this file. Some implementations may impose more restrictions on the format of data-name-1.
The START statement is used with relative files to logically position the file to a specific RECORD.
The file must be declared with SEQUENTIAL or DYNAMIC access mode and opened in INPUT mode or I-O mode.
The START statement does not modify the contents of the record-area. The result of a successful START statement is that the following READ NEXT statement will input the record located by the START statement.
START file-name-1 [KEY relation-expression-1 data-name-1] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-START]
The word KEY is for documentation purpose only.
Relation-condition-1 can be any of the following:
IS EQUAL TO IS = IS GREATER THAN IS > IS NOT LESS THAN IS NOT < IS GREATER THAN OR EQUAL TO IS >=
The words IS, TO, and THAN are for documentation purpose only.
data-name-1 must be the data item specified in the RELATIVE KEY phrase of the ACCESS mode of the associated file control SELECT statement.
If the KEY phrase is not specified, relation IS EQUAL TO is implied.
The INVALID KEY phrase causes imperative-statement-1 to be executed if the START was unsuccessful because no record in file-name-1 satisfies the relation condition.
The NOT INVALID KEY phrase causes imperative-statement-2 to be executed if the START was successful.
The END-START phrase ends the scope of the START statement.
The DELETE statement removes a record from the RELATIVE file.
The RELATIVE file must be opened in I-O mode. The DELETE statement does not modify the contents of the record-area.
For RELATIVE files in SEQUENTIAL mode, the record deleted is the record obtained by the preceding READ statement, which must have been successful.
For RELATIVE files in RANDOM or DYNAMIC mode, the record to be deleted is the record number determined by the value of the RELATIVE KEY data item associated with file-name-1.
DELETE file-name-1 RECORD [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-DELETE]
The words RECORD and KEY are for documentation purpose only.
The INVALID KEY and NOT INVALID KEY phrases must not be used with a RELATIVE file in SEQUENTIAL access mode.
The INVALID KEY phrase causes imperative-statement-1 to be executed if the DELETE was unsuccessful because the record was not available.
The NOT INVALID KEY phrase causes imperative-statement-2 to be executed if the DELETE was successful.
The END-DELETE phrase ends the scope of the DELETE statement.
♣How different OPEN Modes works in different Access modes