|
File Handling in Cobol
File Handling in COBOL
File: A file is a collection of data related to a set of
entities and typically exists on a magnetic tape or a disk.
We refer file as PS in Mainframe environment. In file data
is organized as records. Each record is divided into set of
fields.
For example data related to employee file which consists of
employee ID, employee name, employee Account.
01 WS-EMP-REC.
02 WS-EMP-ID PIC X(07).
02 WS-EMP-NAME PIC X(20).
02 WS-EMP-ACCT PIC X(06).
In this above file data is organized as multiple records each
consists of 33 bytes. Data name WS-EMP-REC is referred as
record. WS-EMP-ID, WS-EMP-NAME and WS-EMP-ACCT is referred as
Fields. Cumulative size of all fields is considered as record
length. A file can be classified as fixed or variable length
files. In fixed length file, size of all records must be same
but in variable length file record length can be vary upon some
fields which are not common. The above referred WS-EMP-REC is an
example for fixed length file.
Files are further classified into 3 types.
1. SEQUENTIAL ORGANIZATION
2. INDEX SEQUENTIAL ORGANIZATION
3. RELATIVE ORGANIZATION
Sequential organization:
In mainframe environment we have 2 types of sequential files.
1. Flat file (NON-VSAM Sequential file)
2. Entry sequential data set (VSAM ESDS)
Both type of files created using different type of Storage
organization.
Sequential files: The records are stored in the file in the
same order in which they are entered. Here, the records can
be accessed only sequentially. To process any record, one
has to read all its preceding records. Further, records
cannot be inserted or deleted. Sequential files are simplest
to handle, they are highly inflexible as they do not
facilitate insertion and deletion of records.
File opened with Extend Mode appends the writing records at
the end of the file. In COBOL program there is no much difference
between these two types. If you are accessing ESDS VSAM file,
then in COBOL program should coded like this.
SELECT FILE ASSIGN TO AS-DDNAME.
Actually DD name matches with JCL DD Name. But in COBOL Program
DDNAMEs should be prefixed with AS- (In case of VSAM ESDS file) .
If you are not doing that then an S213 ABEND might occur when
attempting to open data set.
INDEX SEQUENTIAL ORGANIZATION: Records in this file are stored
based on a key field which is part of record and this is also
called as index. Records in this file are accessible in sequent-
ial , dynamic & random mode. An index sequential file is concep-
tually made up of two files, a data file and an index file.
Though the records are stored in the order in which they are
entered, a sorted index is maintained which relates the key
value to the position of the record in the file and hence provides
a way to access the records both sequentially and randomly.
RELATIVE ORGANIZATION: This file is divided into fixed number
of slots each slot has one record. This is identified as
relative record number. The access method stores and retrieves
a record, based on its relative record number. Records can be
accessed as sequentially or randomly or dynamically. This
relative files faster access compared to other 2 organizations.
But if some of the intermediate records are missing, they occupy
space.
File declaration in COBOL Program:
To make use of files in COBOL program starts in FILE-CONTROL in
ENVIRONMENT DIVISION.
SELECT [OPTIONAL]logical-file-name ASSIGN TO physical-file-name.
[; RESERVE integer {AREA, AREAS}]
[; ORGANIZATION IS SEQUENTIAL]
[; ACCESS MODE IS SEQUENTIAL]
[; FILE STATUS IS data-name]
For example...
SELECT EMPFILE ASSIGN TO EMPFILEO.
Here in COBOL Program we refer this file as EMPFILE but physically
there is no file exists with this name. For any kind of operation
against the file inside the program, make sure you use Logical
name only i.e. EMPFILE. Above sentence EMPFILEO is a mapping that
connects from logical file to physical file. It means whatever
operations we do in COBOL program against the logical file EMPFILE
those will be reflected on Physical file.
In JCL...This file is referred as
//EMPFILEO DD DSN=DG11.CAPHYD.EMPHYD
Physically data stored in DG11.CAPHYD.EMPHYD in system. Operations
against EMPFILE will be reflected in DG11.CAPHYD.EMPHYD.
You must specify SELECT OPTIONAL for those input files that are not
necessarily present each time the object program is executed. Files
with OPTIONAL option can be opened using INPUT,I-O, EXTEND mode.
RESERVE integer {AREA, AREAS}
RESERVE 2 AREAS this instructs system about allocation of buffers
while processing large files stored on disk or tapes, it is ineffi-
cient to read or write single record at a time. Instead, the usual
practice is to group a number of consecutive records to form what
is known as a physical record or a block the number of records in
a block is termed as the blocking factor. There are two advantages
of blocking logical records into a physical record. Firstly, it
results in saving the I/O time required for processing a file and
secondly it results in saving the storage space for a file.
For example to search for a record file has to read sequentially,
if each record is reading from Disk at a time which is time consu-
ming in order to speed the access of records, a couple records are
read from DISK and keep in intermediate storage called buffer. For
sequential access allocation of large block sizes faster the access.
For random access a small block size faster the access of records.
ORGANIZATION IS SEQUENTIAL:
It describes the file organization.
For sequential files ORGANIZATION IS SEQUENTIAL
For Indexed files ORGANIZATION IS INDEXED.
For relative files ORGANIZATION IS RELATIVE.
ACCESS MODE IS SEQUENTIAL:
This sentence identifies the in which mode the file is going to
be accessed.
For sequential access - ACCESS MODE IS SEQUENTIAL
For random access - ACCESS MODE IS RANDOM
For Dynamic access - ACCESS MODE IS DYNAMIC
Dynamic access is a combination of random and sequential access.
FILE STATUS IS data-name:
File status is used to identify the status of each operation that
is performed against the file. For instance. FILE STATUS IS WS-STATUS.
This WS-STATUS data name declares explicitly in Working storage section.
01 WS-STATUS PIC X(02).
After performing each operation on file it is good practice to
check the file status code whether the operation was successful
or not, based on this appropriate action is performed. In file
handling ‘00’ is identified as successful execution.
FILE STATUE Meaning
00 operation sucessful
10 End of record
22 Duplicate key
23 Record Not Found
For more file status code click here
File record description is declared in FILE SECTION.
In file section
FD EMPFILE
[; RECORD CONTAINS integer-1 CHARACTERS]
[; BLOCK CONTAINS integer-2 {RECORDS, CHARACTERS}]
[; DATA {RECORD IS, RECORDS ARE} data-name-1 [, data-name-2] . . .]
01 WS-EMP-REC.
02 WS-EMP-ID PIC X(07).
02 WS-EMP-NAME PIC X(20).
02 WS-EMP-ACCT PIC X(06).
FD is abbreviated from FILE DESCRIPTION.
The RECORD CONTAINS clause specifies the size of the logical records.
Here RECORD CONTAINS 33 CHARCTERS
The BLOCK CONTAINS clause specifies the size of the physical
records. If the records in the file are not blocked, BLOCK
CONTAINS clause can be omitted. When it is omitted, the compiler
assumes that records are not blocked. Even if each physical
record contains only one complete logical record, coding BLOCK
CONTAINS 1 RECORD would result in fixed blocked records.
The DATA RECORD clause specifies the record names defined for the
file. Here DATA RECORD IS WS-EMP-REC.
|
|