INPUT-OUTPUT SECTION


INPUT-OUTPUT SECTION specifies the input and output sources information that is useful to access the resources outside the program. It should start in Area-A, followed by a period.

Syntax -

[INPUT-OUTPUT SECTION.
[FILE-CONTROL paragraph]
[I-O-CONTROL paragraph]]

FILE-CONTROL Paragraph -


FILE-CONTROL paragraph contains complete information about the files used in the program. It maps the logical file (used in program) to the corresponding physical file which is external to the program.

Syntax -

[ENVIRONMENT DIVISION.]
[INPUT-OUTPUT SECTION.]
[FILE-CONTROL.]
    [SELECT [OPTIONAL] logical-file-name ASSIGN TO DSNname]
    [ORGANIZATION IS file-organization]
    [ACCESS MODE IS access-mode]
	[RECORD KEY IS record-key]
	[ALTERNATE RECORD KEY IS alt-record-key WITH DUPLICATES]
	[RELATIVE KEY IS relative-key]
	[RECORD DELIMITER IS record-delimiter]
    [FILE STATUS IS file-status].
Note! All statements coded in [ ] are optional.

Notes -

  • FILE-CONTROL paragraph entry should begin in Area-A. All other entries in the FILE-CONTROL paragraph should start in Area-B.
  • If no files are used in the program, we can ignore the FILE-CONTROL paragraph and all its entries.

SELECT Clause -


SELECT is used to map the logical-file-name with the physical file external to the program.

OPTIONAL

It specifies that the input file is not necessarily available during the execution of the program. Control transfers to the AT END phrase when the first file READ is executed on that file.

logical-file-name -

This is the name we use in the program to refer to the file. It's like an alias for the physical file. (e.g., FILE1, FILE2). It should be unique within this program and same as an FD or SD entry in the DATA DIVISION.

DSNname -

This is the 8-character device name where the file is stored. i.e., DDname in JCL or DSName in CICS. (e.g., DISK, TAPE).

ORGANIZATION clause -


It specifies the file organization to the program, and the file organizations are - SEQUENTIAL, INDEXED, and RELATIVE. If the ORGANIZATION is not coded, assumes the file is sequential. ORGANIZATION is needed for sequential files and not needed for inded and relative files.

The most frequently used file organizations are -

  • SEQUENTIAL - Specifies the file is sequential.
  • INDEXED - Specifies the file is indexed.
  • RELATIVE - Specifies the file is relative.

ACCESS MODE clause -


It defines how the file records are accessed. If it is not coded, sequential access is assumed. ACCESS MODE supports three types of accessing modes -

  • SEQUENTIAL - The records will be accessed sequentially and applicable to sequential, indexed, and relative files.
  • RANDOM - The records will be accessed randomly (in a programmer-specified manner) and applicable to indexed and relative files.
  • DYNAMIC - It is a combination of sequential, random, or both access modes and is applicable to indexed and relative files.

RECORD KEY clause -


The RECORD KEY clause names a key field that provides a unique way to access the record of an indexed file. The RECORD KEY clause is applicable only for indexed files.

record-key - record-key is a variable that contains the key value of the record to access or process it.

ALTERNATE RECORD KEY clause -


ALTERNATE RECORD KEY clause names the alternative key that provides an alternative way to access the record. It is applicable only to indexed files.

alt-key - It specifies the alternative key field and should be part of the record associated with the file.

WITH DUPLICATES - It specifies the file can have duplicates in ALTERNATE KEY field.

RELATIVE KEY clause -


The RELATIVE KEY clause names the relative record number (RRN) to access the records of a relative file.

rel-key -

  • rel-key represents the RRN value.
  • rel-key is not part of the record and should be defined as an unsigned integer variable in the program.
  • rel-key doesn't require for SEQUENTIAL access.
  • rel-key is always required for RANDOM and DYNAMIC accessing.

FILE STATUS clause -


The FILE STATUS clause allows naming a variable used to get the success or failure status of the input-output operations performed on the file.

When the FILE STATUS clause is coded, the system moves a value into the file status variable after each input-output operation performed on the file.

  • file-status (non-vsam files) - It is a two-character alphanumeric variable.
  • file-status (VSAM files - ESDS, KSDS, RRDS) - It is an alphanumeric group variable of 6 bytes.

Refer File Status Codes

Scenario - Declaring a sequential file for sequential access.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT FILE1 ASSIGN TO DISK1
           ORGANIZATION IS SEQUENTIAL
           ACCESS MODE IS SEQUENTIAL 
           FILE STATUS IS WS-FS1.

I-O-CONTROL paragraph -


I-O-CONTROL paragraph specifies the resuming points from where the rerun will be started and the memory area that different files will share. This paragraph is optional in a COBOL program. It can appear only once and should begin in Area A.

Examples -


Scenario1 - Declaring a indexed file for random access.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT FILE2 ASSIGN TO DISK2
           ORGANIZATION IS INDEXED
           ACCESS MODE IS RANDOM 
		   RECORD KEY IS ws-key
		   ALTERNATE RECORD IS ws-alt-key
           FILE STATUS IS WS-FS2.

Scenario2 - Declaring a relative file for dynamic access.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT FILE3 ASSIGN TO DISK3
           ORGANIZATION IS RELATIVE
           ACCESS MODE IS DYNAMIC 
		   RELATIVE KEY IS ws-rrn
           FILE STATUS IS WS-FS3.