EG: ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INFIL ASSIGN TO INFLDD
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS FS-STATUS.
FILE ORGANIZATION
File handling deals with using external or internal files in the Cobol to
write /read/ rewrite/delete the data inside the dataset or file.
Here the input and output source may be one from
• Another Program
• DASD
• Magnetic tape
• Printer, terminal, card reader or puncher
The information in these sources is “physical” record or block
But COBOL can’t handle physical records rather it deals with logical
records
In COBOL a collection of logical records is a file.
File is a collection of relevant records and record is a collection of
relevant fields.
COBOL supports 3 types of files
• Sequential
• Indexed
• Relative Organization
SEQUENTIAL
Flat file
Records are stored in sequential order i.e., one after another
Every record will have predecessor and a successor except for first
and last records
Empty spaces are not allowed
Only forward movement is possible
Records cannot be deleted
The records are added at the EOF.
To access the 21st record we have to read the 20 records
INDEXED
Access of records is easy and fast.
To access we use KEY(S) value(s)
It is a data file
Primary key is stored in sorted fashion.
Records can be deleted.
Records can be accessed sequentially/dynamically.
Keys must be in increasing order of keys.
We cannot update the key field in indexed file.
The deleted record cannot be deleted physically but still can’t be
accessed.
Key can be alphanumeric.
Relative Organization
Files must be disk files.
Records can be accessed by using only RRN (relative record number).
Key should be numeric only.
Random or sequential access can be possible.
Empty spaces are skipped.
If we know the key value of a record then it’s better to use RRDS.
THIS KNOWLEDGE HELPS US TO WRITE ORGANIZATION IN FILE-CONTROL.
NOW TO WRITE ACCESS MODE WE READ THE FOLLOWING.
ACCESSING MODES
• Sequential
• Random
• Dynamic
FILE HANDLING
We have 5 steps here
1.ALLOCATION
2.DEFINITION
3.OPEN
4.PROCESS
5.CLOSE
ALLOCATION
Declared in file-control paragraph under environment division
DEFINITION
Defined in file section under data division
Open
Data set is connected and read into program using OPEN statement.
PROCESS
Using I-O statements provided by COBOL
Can be processed as per your requirement like write, read, rewrite
and delete
CLOSE
After processing the file, close the file to disconnect it from the
program.
Let us see an example of cobol program where we can see
everything
STEP 1:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBEX01
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*INPUT FILE
SELECT TI001-INPUT ASSIGN TO INCUSPS
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-FILE-STATUS1.
As we discussed above the first step is allocation i.e., declared under
the file-control of environment division.
Here TI001-INPUT is called file descriptor it is programmer’s choice.
INCUSPS is the ddname which we mentioned in jcl program this is
here used as a logical record which can be processed by cobol
We know about organization and access mode and next we have is
file status we will see this in detail in next steps.
STEP 2: DEFINITION
We do this in file section of data division.
Here we declare all the fields of the file that matches it with it’s
record length.
DATA DIVISION.
FILE SECTION.
FD TI001-INPUT. ------( –this is a field name )
01 TI001-INPUT-RECORD.
05 TI001-ACC-NUM PIC X(10). ––this is a field name
05 FILLER PIC X(01).
05 TI001-ACC-NAME PIC X(20). –this is a field name
05 FILLER PIC X(01).
05 TI001-ACC-TYPE PIC X(10). ––this is a field name
05 FILLER PIC X(01).
05 TI001-CREDIT-SCORE PIC 9(1). ––this is a field name
05 FILLER PIC X(01).
05 TI001-LOAN-AMOUNT PIC 9(5).9(2). ––this is a field name
05 FILLER PIC X(01).
05 TI001-CURR-BAL PIC 9(6).9(2). ––this is a field name
05 FILLER PIC X(17).
FD TO001-OUTPUT.
01 TO001-OUTPUT-RECORD.
05 TO001-ACC-NUM PIC X(10).
05 FILLER PIC X(01).
05 TO001-ACC-NAME PIC X(20).
05 FILLER PIC X(01).
05 TO001-ACC-TYPE PIC X(10).
05 FILLER PIC X(01).
05 TO001-CREDIT-REPORT PIC A(10).
05 FILLER PIC X(01).
05 TO001-LOAN-AMOUNT PIC 9(5).9(2).
05 FILLER PIC X(18).
WORKING-STORAGE SECTION. -----(here we define the file status
01 WS-G-FILE-STATUS. Bcz it is just a variable not a
05 WS-FILE-STATUS1 PIC X(02). File)
88 WFS1-SUCCESS VALUE '00'.
88 WFS1-EOF VALUE '10'.
We us the status of file to know the result of a operation like add or
move if it is successful we will get 00 if not it results an error so to
know the status of file after operation the file status is used.
*Generally after these two steps we code procedure division and
there we start all the paras so the next two seps are para executions
so make a note that the following are paragraphs and comes under
procedure division*
STEP 3: OPEN
2100-FILE-OPEN-PARA.
OPEN INPUT TI001-INPUT --- here we are opening the input
EVALUATE TRUE file with OPEN statement
WHEN WFS1-SUCCESS --- checking the status of file after
CONTINUE opening the file
WHEN OTHER
DISPLAY "INPUT FILE OPEN FAILED"
DISPLAY 'FILE STATUS' WS-FILE-STATUS1
END-EVALUATE
OPEN OUTPUT TO001-OUTPUT --- opening output file
EVALUATE TRUE
WHEN WFS2-SUCCESS --- checking the status of file after
CONTINUE opening the file
WHEN OTHER
DISPLAY "OUTPUT FILE OPEN FAILED"
DISPLAY 'FILE STATUS' WS-FILE-STATUS1
END-EVALUATE.
2100-FILE-OPEN-PARA-EXIT.
EXIT.
Make sure you close the evaluate statement at the end.
STEP 4 : PROCESS
THIS IS PURELY AUTHOR’S CHOICE WRITING OR MAKING ARITMETIC
OPERATIONS
HERE WE READ THE INPUT FROM INPUT FILE AND PROCESS IT BASED
ON OUR REQUIRMENTS AND WRITE IT IN THE OUTPUT FILE.
SO READ AND WRITE ARE MANDATORY BUT IN MIDDLE ARE BASED
ON OUR REQUIRMNETS
STEP 5 : CLOSE
9100-CLOSE-PARA.
CLOSE TI001-INPUT -- closing the file with command CLOSE
EVALUATE TRUE
WHEN WFS1-SUCCESS
DISPLAY 'INPUT FILE CLOSED'
WHEN OTHER
DISPLAY 'INPUT FILE NOT CLOSED'
DISPLAY WS-FILE-STATUS1
END-EVALUATE.
CLOSE TO001-OUTPUT --- closing the file using close command
EVALUATE TRUE
WHEN WFS2-SUCCESS
DISPLAY 'OUTPUT FILE CLOSED'
WHEN OTHER
DISPLAY 'OUTPUT FILE NOT CLOSED'
DISPLAY WS-FILE-STATUS2
END-EVALUATE.
9100-CLOSE-PARA-EXIT.
EXIT.
This is for non- VSAM datasets
Now we will see about vsam datasets in file handling
VSAM FILE HANDLING
Here for VSAM files the file-control changes we will see it in details
FOR ESDS
SELECT TO001-OUTPUT ASSIGN TO AS-OUTESDS
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-FILE-STATUS2.
In ESDS the records are organized in sequential manner and the
access mode is also sequential so we can use the same lines of non-
vsam files but to differentiate it from non-vsam files we use “AS-“
We can use it as input or output
KSDS
SELECT TO001-OUTPUT ASSIGN TO OUTKSDS
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS TO001-ACC-NUM
FILE STATUS IS WS-FILE-STATUS2.
In KSDS the records are stored in indexed fashion and they can be
accessed in dynamic mode as well based on the accessing
requirment we can change it but organization is indexed and as we
know the records in KSDS can be accessed through KEYS so keys
should be mentioned in the file-control.
RRDS
*INPUT FILE
SELECT TI001-INPUT ASSIGN TO INPRRDS
ORGANIZATION IS RELATIVE
ACCESS MODE IS DYNAMIC
RELATIVE KEY IS WS-RRN-NUM
FILE STATUS IS WS-FILE-STATUS1.
Here the organization is relative and dynamic access is possible as we
know we can access records of RRDS using RRN that is relative
number so we declare it as relative key
****WE CAN ACCESS VSAM FILES USING ALTERNATE OR SECOND KEY
AS WELL****
*INPUT FILE
SELECT TI001-INPUT ASSIGN TO INPKS
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS TI001-ACC-NUM
ALTERNATE RECORD KEY IS TI001-ACC-NAME WITH
DUPLICATES
FILE STATUS IS WS-FILE-STATUS1.