COBOL File Declaration

COBOL File Processing cycle COBOL File Open

Advertisements


FILE Declaration specifies the FILE specifications to the program.

FILE Declaration includes allocating & defining of FILE.

FILE Declaration divided into two categories.


  1. FILE Allocation
  2. FILE Definition

1. FILE Allocation:


FILE using in the program should be declared in FILE-CONTROL section of ENVIRONMENT DIVISION.

This declaration had a mapping with JCL through DDNAME provided in the program.

The DDNAME in the program and the JCL provided should be the same.

The FILE allocation will be done for the file declared in ENVIRONMENT DIVISION.


Syntax:

			
ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
     FILE-CONTROL.
			
	SELECT [OPTINAL] filename ASSIGN TO ddname.
	ORGANIZATION IS {SEQUENTIAL/INDEXED/RELATIVE}.
	[ACCESS IS {SEQUENTIAL/RANDOM/DYNAMIC}].
	[RECORD KEY IS ws-key].
	[RELATIVE KEY IS ws-rel-rrn].
	[ALTERNATE RECORD KEY IS ws-key {WITH/WITHOUT} DUPLICATES].
	[FILE STATUS IS ws-status]

In the above, all other are optional except SELECT and ORGANIZATION.


SELECT:


The select statement is mandatory for the file declaration.


OPTIONAL:

This keyword OPTIONAL is mainly used to recognize the file declared in the program as a dummy file and no DDNAME required to be coded in the JCL.

During the runtime of the program it won’t through any error and the execution will be successfully completed.

i.e. If the OPTIONAL parameter coded in the program, it won’t check for the DDNAME in JCL.

If the OPTIONAL parameter is not coded in SELECT, then it will check the DDNAME in JCL during the runtime.


ASSIGN TO:

ASSIGN TO Clause is used to map the DDNAME between the program and JCL.

DDNAME in the JCL is the physical assignment of memory through a name, provided in JCL.

FILENAME defined in the program will assign to the DDNAME through the SELECT - ASSIGN TO Clause.

ASSIGN TO clause will create a link between the logical file in the program and physical file provided in JCL through the unique DDNAME provided in Program and as well as JCL.

The DDNAME in the program and JCL should be same always.


ORGANIZATION:

The ORGANIZATION clause Optional clause.

ORGANIZATION clause used to differentiate the file types.

ORGANIZATION clause can be of three types.


  1. Sequential -> PS or VSAM ESDS
  2. Relative -> VSAM RRDS
  3. Indexed -> VSAM KSDS

By default, the ORGNIZATION is sequential.


ACCESS MODE:

ACCESS MODE is mainly used to define the accessing way of the file based the requirement in the program. (i.e. how the data required to read from the file)

There are 3 access modes mainly. Let’s discuss about them in detail


1. Sequential:

It’s a self explanatory that the records in the file can be accessed in a sequential manner. i.e. from starting onwards, one after the other.

This access mode used for SEQUENTIAL files only.

It is the default accessing mode if the ACCESS MODE clause was not defined in the program


Adv/Disadvantages:


When the file has huge number of records, it will take too much time to read a specific record from the file.

This is very effective when the file has less number of records.


2. Random:

It’s also self explanatory that the records can be accessed randomly. i.e. directly by providing the key.

This access mode can be used for INDEXED and RELATIVE files only.


Adv/Disadvantages:


This access mode is very effective in comparing with other accessing modes.


3. Dynamic:

It’s also self explanatory and a combination of above two access modes.

By using this access mode, the file can be accessed in sequentially and randomly.

This access mode can be used for all types of files.


FILE STATUS:

FILE STATUS clause is used to check the status immediately after the operation performed on the file.

WS-STATUS field should be defined as X(02) in working-storage section.

If the FILE STATUS defined in the program, then once after the operation performed on the file, the status will be stored in the respective working storage field provided during the declaration.

The FILE STATUS field can be validate by using IF condition.


2. Definition of File:


The file which is going to be used in the program should be declared in FILE-CONTROL section of ENVIRONMENT DIVISION.


Syntax:

FD FILENAME.
	RECORD CONTAINS A{ TO B} CHARACTERS.
	BLOCK CONTAINS C { TO D} CHARACTERS/RECORDS.
	RECORDING MODE IS F/FB/V/VB.
	DATA RECORD IS FILE-REC.
 	01 FILE-REC PIC X(N).

Let’s discuss about one by one from the definition file.


RECORD CONTAINS:

This is to specify the record length of the files in bytes.

There are two types of definitions to the two types of files.

For Fixed length files, the definition would be


RECORD CONTAINS A CHARACTERS.
			    * A- is the length of record

For variable length files, the Definition would be.


RECORD CONTAINS A TO B CHARACTERS. 
		* A - Minimum length of record
		* B – Maximum length of record.

BLOCK CONTAINS:

This is to specify the physical record length of the files.

There are two types of definitions to the two types of files.

For Fixed length files, the definition would be


BLOCK CONTAINS C CHARACTERS.    
		* C- is the physical length of record

For variable length files, the Definition would be


BLOCK CONTAINS C TO D CHARACTERS. 
		* C - Minimum length of record
		* D – Maximum length of record.

Block contains is not applicable for VSAM files.


Practical Example:


Code:


  IDENTIFICATION DIVISION.                                        
  PROGRAM-ID. SEQFILE.                                             
  ENVIRONMENT DIVISION.                                           
  INPUT-OUTPUT SECTION.                                            
  FILE-CONTROL.                                                    
      SELECT FILE1 ASSIGN TO DISK1.                                
      ORGANIZATION IS SEQUENTIAL                                  
      ACCESS MODE IS SEQUENTIAL                                    
      FILE STATUS IS WS-FS.                                        
  DATA DIVISION.                                                  
  FILE SECTION.                                                    
      RECORD CONTAINS 80 CHARACTERS.                               
      BLOCK CONTAINS 800 CHARACTERS.                               
      RECORDING MODE IS F.                                        
      DATA RECORD IS STD-REC.                                     
  FD  FILE1.                                                      
  01 STD-REC.                                                     
      02 STD-NO          PIC 9(03).                                
      02 STD-NAME        PIC X(20).                                
      02 STD-GENDER      PIC X(07).                                
      02 FILLER          PIC X(50).                                
  WORKING-STORAGE SECTION.                                         
  77 WS-FS               PIC 9(02).                                
  01 WS-EOF-SW           PIC X(01) VALUE 'N'.                      
     88 EOF-SW           VALUE 'Y'.                                
     88 NOT-EOF-SW       VALUE 'N'.                                
  PROCEDURE DIVISION.                                              
      DISPLAY 'SEQUENTIAL FILE READING...'.                        
      OPEN INPUT FILE1.                                           
      PERFORM UNTIL EOF-SW                                         
         READ FILE1                                                
         AT END MOVE 'Y' TO WS-EOF-SW                              
         DISPLAY 'RECORD READ : ' STD-REC                          
      END-PERFORM.                                                 
      CLOSE FILE1.                                                 
      DISPLAY STD-REC.                                            
      STOP RUN.                                                   


COBOL File Processing cycle COBOL File Open

Advertisements