COBOL Input Output Section

COBOL Configuration Section COBOL Data Division

Advertisements


INPUT-OUTPUT SECTION links the identifiers from the program to external program resources like files etc,.

INPUT-OUTPUT SECTION relates the file-names defined to the corresponding file-names known to the operating system.

FILE-CONTROL:

FILE-CONTROL section contains the complete information about the files used in the program.

FILE-CONTROL contains information of files which are used in Program like From where the file needs to be accessed like Disk, tape, How the reading can be like sequential, random, dynamic etc,.

FILE-CONTROL contains the below list of clauses

	1.	SELECT
	2.	ACCESS MODE
	3.	RECORD KEY
	4.	ALTERNATE RECORD KEY
	5.	ORGANIZATION
	6.	FILE STATUS

In FILE-CONTROL body, the SELECT clause must be specified first.

SELECT Clause

The SELECT clause is used to identify a file used in program.

The SELECT clause describes from where the file existed and data can be read.

Syntax:
SELECT [Optional] file-name ASSIGN {[TO file-info-1][USING data-name-1]}

OPTIONAL:

This phrase is allowed to specify an input file in the SELECT statement that may not be present in JCL during a particular execution of the program.

If this phrase was specified and the first READ for file is executed, the imperative statement AT END phrase is executed.

If no AT END phrase has been specified, then a USE procedure must be defined and the USE procedure will be executed.

Note:  If the file is not present and the file is opened in I-O/extend mode with the OPTIONAL phrase, a new file will be created. Otherwise, file status 35 will be returned.


ASSIGN:

The ASSIGN clause associates a file with the storage medium on where the file will going to store.

The ASSIGN clause specifies the target of storage like DISK, TAPE etc,.


ACCESS MODE Clause

The ACCESS MODE specifies the way how the program can access the associated file.

The ACCESS MODE contains majorly three types.

	      1. Sequential 
		– Accessing file sequentially.
		- Used to read Sequential files/indexed files/Relative files.
			
	      2. Dynamic
		- Accessing file dynamically.
		- Used to read indexed files/Relative files.
			
	      3. Random 
		– Accessing file randomly.
		- Used to read indexed files/Relative files.

Syntax:
ACCESS MODE IS {SEQUENTIAL/DYNAMIC/RANDOM} [RELATIVE KEY IS data-name]

The below are the syntaxes for the three types of files:

		1)	Sequential files
			ACCESS MODE IS SEQUENTIAL
				
		2)	Relative files
			ACCESS MODE IS {SEQUENTIAL/DYNAMIC/RANDOM} 
			[RELATIVE KEY IS data-name]
				
		3)	Indexed files
			ACCESS MODE IS {SEQUENTIAL/DYNAMIC/RANDOM}

RECORD KEY:

The RECORD KEY clause is required and used for only indexed files.

The RECORD KEY should be unique (primary key) and part of index.

The RECORD KEY doesn’t allow the duplicates in the index key.

Syntax:
	RECORD KEY IS data-name-1

ALTERNATE RECORD KEY:

ALTERNATE RECORD KEY names an alternate record key for an indexed file.

The number of alternate keys for a file must be the same as the number used when the file was created.

ALTERNATE RECORD KEY WITH DUPLICATES specifies the file can have duplicates in ALTERNATE KEY.

Syntax:
	[ALTERNATE RECORD KEY IS data-name-2 [WITH DUPLICATES]]...

ORGANIZATION:

The ORGANIZATION clause describes the logical structure of the file provided.

The ORGANIZATION clause is required for relative and indexed files.

If ORGANIZATION clause not specified, by default it will be consider as Sequential files. It is optional for sequential files.

Syntax:
	[ORGANIZATION IS] SEQUENTIAL/RELATIVE/INDEXED 

FILE STATUS:

The FILE STATUS clause allows naming a data item used in getting information about the success/failure of input-output operations performed on the file specified.

FILE STATUS gives the advantage of not to declare the data item specially in Working-storage section.

This is an optional clause and used for all types of files except sort/merge files.

Syntax:
		[FILE STATUS IS stat-item].

INPUT-OUTPUT SECTION Practical Example:

The simple input-output section program example was shown below


Let’s take an example of reading a Sequential file data and display it through program.

Assume that MTH.EMP.DATA has the employee details.

Let us discuss step by step how the file declaration can be in the program.

1. SELECT FILE1 ASSIGN TO DISK1.

The above SELECT clause has a logical name FILE1 to link the layout in the DATA DIVISION to access the records.

DISK1 is a logical name which used to link the file provided in JCL. The name in ASSIGN CLAUSE should be same as the DDNAME in JCL.

Program: - SELECT FILE1 ASSIGN TO DISK1.

JCL: - //DISK1 DD MTH.EMP.DATA,DISP=SHR

2. ORGANIZATION IS SEQUNETIAL

File we are using in the program is sequential. So the ORGANIZATION should be sequential.

3. ACCESS MODE IS SEQUENTIAL

For the sequential files ACCESS MODE should be SEQUENTIAL.

4. FILE STATUS IS FS1.

In the below example, you can see the JCL, Input file and OUTPUT of the program.



If any questions on the above, post your queries on Cobol Discussion forum


COBOL Configuration Section COBOL Data Division

Advertisements