The input-output section specifies the file organization and access methods. In addition, the input-output section contains the FILE-CONTROL paragraph and I-O-CONTROL paragraph.

Syntax -

[INPUT-OUTPUT SECTION.
[file-control-paragraph]
[i-o-control-paragraph]]

INPUT-OUTPUT SECTION relates the files defined in the program to the corresponding files in the JCL by using file-names.

For example -

INPUT-OUTPUT SECTION file mapping

INPUT-OUTPUT SECTION should start in Area-A, followed by a period.

For example -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       ENVIRONMENT DIVISION.                                            
       INPUT-OUTPUT SECTION.                                            
       FILE-CONTROL.   

file-control-paragraph -

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

FILE-CONTROL paragraph has the following formats are for file entries -

  • Sequential file entries (QSAM, VSAM)
  • Indexed file entries (VSAM)
  • Relative file entries (VSAM)
  • Line-sequential file entries (Limited to Z/oS UNIX)

Syntax -

Sequential file control entry -

Sequential file control Syntax

Indexed file control entry -

Indexed file control Syntax

Relative file control entry -

Relative file control Syntax

FILE-CONTROL contains file accessing location, file accessing type, file key, file alternative key, other information of file associated with the COBOL program.

For example -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT FILE1 ASSIGN TO DISK1                                 
           ORGANIZATION IS SEQUENTIAL                                   
           ACCESS MODE IS SEQUENTIAL                                    
           FILE STATUS IS WS-FS1.

FILE-CONTROL should begin in Area-A and be followed by a period.

If no files are defined in the program, we can ignore the FILE-CONTROL paragraph and all its clauses; even FILE-CONTROL is specified.

For example -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.         --> this gets ignored.
       DATA DIVISION. 

It should contain one entry for each file described in an FD or SD entry in the DATA DIVISION.

For example - FILE1 is physical file and has FD entry in the FILE SECTION. For each FD entry, it should have a one-to-one entry in FILE-CONTROL.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT FILE1 ASSIGN TO DISK1
           .....
           .....
		   
       DATA DIVISION.                      
       FILE SECTION.                       
       FD  FILE1                           
           RECORD CONTAINS 80 CHARACTERS   
           .....
           .....

The SELECT clause should appear first in FILE-CONTROL entry for a specific file. The other clauses can appear in any order and immediately follows the RECORD KEY or ALTERNATE RECORD KEY clauses to which those are associated.

For example -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT FILE1 ASSIGN TO DISK1 
           RECORD KEY IS WS-REC-KEY1
           ALTERNATE RECORD KEY IS WS-ALT-KEY1		   
           ORGANIZATION IS INDEXED                                  
           ACCESS MODE IS DYNAMIC                                    
           FILE STATUS IS WS-FS1.

FILE-CONTROL paragraph should start in Area-A. All other clauses in the FILE-CONTROL paragraph should start in Area-B and should end with a period.

SELECT Clause

The SELECT clause is used to identify a file associated with an external data set in the program.

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT [OPTIONAL] file-name ASSIGN TO jcl-file-name

OPTIONAL

  • OPTIONAL caluse specifies only for files opened in the input, I-O, or extend mode.
  • This phrase specifies that the input file is not necessarily available during the execution of the program.
  • If OPTIONAL phase coded and the first file READ is executed, control transfers to the AT END phrase and executed.
  • If no AT END phrase has been coded and the USE procedure defined, the USE procedure gets executed.

For example -If the optional specified with SELECT, then the physical file is not required to exist. OPTIONAL clause generally uses when DUMMY files used like below

In program -
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT OPTIONAL FILE1 ASSIGN TO DISK1
		   
In JCL -

//DISK1    DD  DUMMY      
			or
//DISK1    DD  DSN=MATEGJ.INPUT.FILE01,DISP=SHR  

In the above example, MATEGJ.INPUT.FILE01 file may not require to physically exist.

file-name

  • It should be the same as an FD or SD entry in the DATA DIVISION.
  • A file-name should follow the rules of a COBOL user-defined name and should be unique within this program.
  • The ASSIGN clause can follow the SELECT clause when the file-name specifies a sort or a merge file.

For example -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT FILE1 ASSIGN TO DISK1         
           .....
           .....
		   
       DATA DIVISION.                      
       FILE SECTION.                       
       FD  FILE1                           
           RECORD CONTAINS 80 CHARACTERS   
           .....
           .....

ASSIGN

  • The ASSIGN clause associates with jcl-file-name and specifies the actual external name of the file from the JCL.

For example -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT FILE1 ASSIGN TO DISK1        
           .....
           .....

jcl-file-name (dd-name)

  • jcl-file-name identifies the external file name(ddname) in the run JCL.
  • It can be specified as a name or as an alphanumeric literal.
  • jcl-file-name is not the name of a data item and cannot contain an underscore character.

For example -

In program -
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT FILE1 ASSIGN TO DISK1
		   
In JCL -

//DISK1    DD  DSN=MATEGJ.INPUT.FILE01,DISP=SHR  

ORGANIZATION clause

The ORGANIZATION clause describes the logical structure of the file specified in the program. The logical structure is used during the file creation and can't be changed once the file is created.

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION IS [SEQUENTIAL]/[INDEXED]/[RELATIVE]

File organization is the permanent logical structure of the file. The ORGANIZATION clause is required for relative and indexed files.

ORGANIZATION is optional for sequential files. If the ORGANIZATION clause is not specified, the compiler assumes ORGANIZATION IS SEQUENTIAL (sequential file).

The most frequently used file organizations specified below -

  • SEQUENTIAL - Specifies the file is sequential.
    For example -
               ORGANIZATION IS SEQUENTIAL 
  • INDEXED - Specifies the file is indexed.
    For example -
               ORGANIZATION IS INDEXED
  • RELATIVE - Specifies the file is relative.
    For example -
               ORGANIZATION IS RELATIVE

RECORD DELIMITER clause

The RECORD DELIMITER clause specifies the method of determining the length of a variable-length record for the file on an external medium. It specifies only for variable-length records. The RECORD DELIMITER clause has no effect on the execution of the program.

STANDARD-1 - If STANDARD-1 is specified, the external medium should be a magnetic tape file.

assignment-name - Can be any COBOL word that is using as a delimiter.

ACCESS MODE clause

The ACCESS MODE clause defines how the records of the file are available for processing. If the ACCESS MODE clause is not specified, sequential access is assumed.

ACCESS MODE has below three accessing modes -

  • SEQUENTIAL
  • DYNAMIC
  • RANDOM

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE IS [SEQUENTIAL]/[DYNAMIC]/[RANDOM]

SEQUENTIAL -

SEQUENTIAL ACCESS MODE allows reading and writing records of a file in a serial manner. This MODE is used for sequential files/indexed files/relative files.

For Example -

       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE IS SEQUENTIAL

DYNAMIC -

DYNAMIC ACCESS MODE allows the specific input-output statement to determine the access mode. Records can be processed sequentially or randomly, or both. This MODE is used for indexed files/relative files.

For Example -

       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE IS DYNAMIC

RANDOM -

RANDOM ACCESS MODE allows reading and writing records in a programmer-specified manner. This MODE is used for indexed files/relative files.

For Example -

       FILE-CONTROL.
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE IS RANDOM

RECORD KEY clause

The RECORD KEY clause specifies the data item (key), which is the part of the record for an indexed file. The RECORD KEY data item values should be unique among records in the file and don’t allow duplicates. The RECORD KEY clause is required and used for only indexed files.

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE ......
           RECORD KEY IS file-key

file-key

  • file-key specifies the RECORD KEY data item.
  • file-key is part of the record associated with the file.
  • The key is treated as an alphanumeric item regardless of the category of the key data item.
  • The file-key should not reference a variable-length data item.
  • data-name-2 can be qualified.
  • If the indexed file contains variable-length records, file-key can exceed the minimum record size, but this is not recommended.

ALTERNATE RECORD KEY clause

Specifies a data item within the record that provides an alternative way to access the data in an indexed file.

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE ......
           RECORD KEY ......
           ALTERNATE RECORD KEY IS alt-key [WITH DUPLICATES]

alt-key

  • alt-key specifies the ALTERNATE RECORD KEY variable(s).
  • alt-key should be part of the record associated with the file.
  • The key is treated as an alphanumeric type regardless of the key variable type.
  • If the indexed file contains variable-length records, alt-key can exceed the minimum record size, but this is not recommended.

WITH DUPLICATES

  • WITH DUPLICATES specifies the alternative key can contain duplicates.
  • If the DUPLICATES phrase is specified, the values contained in the ALTERNATE RECORD KEY variable(s) can have duplicates among records in the file.
  • In sequential access, all the records with duplicate keys are retrieved in the same order they placed in the file. In random access, only the first record with a duplicate key can be retrieved.
  • If the DUPLICATES phrase is not specified, the values contained in the ALTERNATE RECORD KEY variable should be unique among records in the file.

RELATIVE KEY clause

The RELATIVE KEY clause identifies the relative record number for a specific logical record within a relative file.

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE ......
           RELATIVE KEY IS rel-key

rel-key

  • rel-key should be defined in the program as an unsigned integer data item.
  • rel-key is not part of the record.
  • rel-key don’t require for ACCESS IS SEQUENTIAL but only needed if the START statement is to be used.
  • rel-key is always needed for ACCESS IS RANDOM, and ACCESS IS DYNAMIC.
  • When the START statement is executed, the system uses the RELATIVE KEY data item contents to determine the record from where the sequential processing is to begin.
  • If a value assigned to rel-key and a START statement is not executed, processing begins from the first record in the file and the rel-key value ignored.
  • The ACCESS MODE IS RANDOM clause should not be specified for file-names specified in the USING or GIVING phrase of a SORT or MERGE statement.

PASSWORD clause

The PASSWORD clause controls the file access.

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE ......
           RECORD KEY .....
           PASSWORD IS pw-code

pw-code

  • pw-code specifies the password.
  • Each pw-code should be defined as a type alphabetic, alphanumeric, or alphanumeric-edited variable in the WORKING-STORAGE SECTION.
  • The first eight characters are used as the password, or if shorter than eight(8) characters, the field is padded with blanks to eight characters.
  • When the PASSWORD clause is specified, the PASSWORD data item should contain a valid password for the file.
  • The PASSWORD clause is not valid for QSAM sequential files.
  • If the PASSWORD clause is specified for VSAM files, it should immediately follow the RECORD KEY or ALTERNATE RECORD KEY clause.

FILE STATUS clause

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

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

Syntax -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       FILE-CONTROL.                                                    
           SELECT .....
           ORGANIZATION ......
           ACCESS MODE ......
           RECORD KEY .....
           FILE STATUS file-status

The value indicates the execution status of the statement.

file-status (non-vsam files)

The file status key is a two-character alphanumeric variable defined in the WORKING-STORAGE, LOCAL-STORAGE or LINKAGE SECTION. file-status should not contain the PICTURE symbol 'P'. In addition, the variable cannot contain an OCCURS DEPENDING ON clause.

For Example -

       FILE-CONTROL.
           SELECT FILE1 ASSIGN TO DISK1 		   
           ORGANIZATION IS SEQUENTIAL                                  
           ACCESS MODE IS SEQUENTIAL                                    
           FILE STATUS IS WS-FS1.
		   .....
		   .....
		   DATA DIVISION.
		   WORKING-STORAGE SECTION.
		   01 WS-FS1      PIC X(02).

file-status (VSAM files)

The file status key is an alphanumeric group item of 6 bytes defined in the WORKING-STORAGE SECTION or LINKAGE SECTION. Specify file-status only if the file is a VSAM file (ESDS, KSDS, RRDS).

For Example -

       FILE-CONTROL.
           SELECT FILE1 ASSIGN TO DISK1 		   
           ORGANIZATION IS INDEXED                                  
           ACCESS MODE IS DYNAMIC                                    
           FILE STATUS IS WS-FS2.
		   .....
		   .....
		   DATA DIVISION.
		   WORKING-STORAGE SECTION.
		   01 WS-FS2      PIC X(06).

File-status holds the 6-byte VSAM return code is a combination of below -

  • The first 2 bytes of file-status contain the VSAM return code in binary format. The value for this code is defined (by VSAM) as 0, 8, or 12.
  • The next 2 bytes of file-status contain the VSAM function code in binary format. The value for this code is defined (by VSAM) as 0, 1, 2, 3, 4, or 5.
  • The last 2 bytes of file-status contain the VSAM feedback code in binary format. The code value is 0 through 255.

If VSAM returns a nonzero return code, file-status is set. Refer File Status Codes

i-o-control-paragraph -

I-O-CONTROL specifies when the storage areas to be shared by different files and checkpoints are to be taken.

This paragraph is optional in a COBOL program. I-O-CONTROL can appear only once at the beginning of the paragraph.

I-O-CONTROL should begin in Area-A, ends with a separator period.


Practical Example -

Scenario - Below example describes how the INPUT-OUTPUT SECTION used in COBOL programming. Below program coded to check the record existance using the S.NO from the file.

File Layout -

INPUT-OUTPUT SECTION Copybook

File Input -

INPUT-OUTPUT SECTION File Input

Code -

INPUT-OUTPUT SECTION program code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. IOSECTIN.                                            
       AUTHOR. MTH.                                                     
                                                                        
       ENVIRONMENT DIVISION.                                            
       INPUT-OUTPUT SECTION.                                            
       FILE-CONTROL.                                                    
           SELECT FILE1 ASSIGN TO DISK1                                 
           ORGANIZATION IS SEQUENTIAL                                   
           ACCESS MODE IS SEQUENTIAL                                    
           FILE STATUS IS WS-FS1.                                       
                                                                        
       DATA DIVISION.                                                   
       FILE SECTION.                                                    
       FD  FILE1                                                        
           RECORD CONTAINS 80 CHARACTERS                                
           BLOCK CONTAINS 800 CHARACTERS                                
           RECORDING MODE IS F                                          
           DATA RECORD IS STD-REC.                                      
       01 STD-REC.                                                      
           02 STD-NO          PIC X(03).                                
           02 STD-NAME        PIC X(20).                                
           02 STD-GENDER      PIC X(07).                                
           02 FILLER          PIC X(50).                                
                                                                        
       WORKING-STORAGE SECTION.                                         
       01 WS-STD-REC          PIC X(80).                                
       01 WS-VAR.                                                       
           05 WS-STD-NO           PIC X(03).                            
           05 WS-STD-NAME         PIC X(20).                            
           05 WS-STD-GENDER       PIC X(07).                            
           05 WS-FS1              PIC 9(02).                            
           05 WS-EOF-SW           PIC X(01) VALUE 'N'.                  
              88 EOF-SW           VALUE 'Y'.                            
              88 NOT-EOF-SW       VALUE 'N'.                            
           05 WS-REC-FOUND        PIC X(01) VALUE 'N'.                  
              88 REC-FOUND        VALUE 'Y'.                            
              88 REC-NOT-FOUND    VALUE 'N'.                            
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           ACCEPT WS-STD-NO.                                            
           ACCEPT WS-STD-NAME.                                          
           ACCEPT WS-STD-GENDER.                                        
                                                                        
           OPEN INPUT FILE1.                                            
                                                                        
           PERFORM UNTIL EOF-SW OR REC-FOUND                            
              READ FILE1 INTO WS-STD-REC                                
              AT END MOVE 'Y' TO WS-EOF-SW                              
              NOT AT END MOVE WS-STD-REC TO STD-REC                     
                 IF STD-NO = WS-STD-NO                                  
                    MOVE 'Y' TO WS-EOF-SW                               
                    MOVE 'Y' TO WS-REC-FOUND                            
                    DISPLAY 'MATCHED REC: ' STD-REC                     
                 END-IF                                                 
              END-READ                                                  
           END-PERFORM.                                                 
                                                                        
           IF REC-NOT-FOUND                                             
              DISPLAY 'REQUESTED RECORD NOT FOUND'                      
           END-IF.                                                      
                                                                        
           CLOSE FILE1.                                                 
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

JCL for Matching Record -

JCL

Output for Matching Record -

INPUT-OUTPUT SECTION Matching Record Output

JCL for Non-Matching Record -

JCL

Output for Non-matching Record -

INPUT-OUTPUT SECTION Non-matching Record Output