Cobol File Handling
Cobol File Handling
COBOL
Topics covered in the Session
(1) Introduction to File handling.
(2) File ORGANIZATION and ACCESS methods.
(3) File handling verbs.
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
We use the term FIELD to describe an item of We use the term FIELD to describe an item of
information we are recording about an object information we are recording about an object
(e.g. StudentName, DateOfBirth, CourseCode). (e.g. StudentName, DateOfBirth, CourseCode).
We use the term RECORD to describe the collection of We use the term RECORD to describe the collection of
fields which record information about an object fields which record information about an object
(e.g. a StudentRecord is a collection of fields (e.g. a StudentRecord is a collection of fields
recording information about a student). recording information about a student).
We use the term FILE to describe a collection of one or We use the term FILE to describe a collection of one or
more occurrences (instances) of a record type more occurrences (instances) of a record type
(template). (template).
It is important to distinguish between the record It is important to distinguish between the record
occurrence (i.e. the values of a record) and the record occurrence (i.e. the values of a record) and the record
type (i.e. the structure of the record). type (i.e. the structure of the record).
Every record in a file has a different value but the same Every record in a file has a different value but the same
structure. structure.
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
Files, Records,
Files, Records,
Fields.
Fields.
StudId StudName DateOfBirth StudId StudName DateOfBirth
9723456 COUGHLAN 10091961
9724567 RYAN 31121976
9534118 COFFEY 23061964
9423458 O'BRIEN 03111979
9312876 SMITH 12121976
StudId StudName DateOfBirth StudId StudName DateOfBirth
9723456 COUGHLAN 10091961
9724567 RYAN 31121976
9534118 COFFEY 23061964
9423458 O'BRIEN 03111979
9312876 SMITH 12121976
STUDENTS STUDENTS
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudId PIC 9(7).
02 StudName PIC X(8).
02 DateOfBirth PIC X(8).
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudId PIC 9(7).
02 StudName PIC X(8).
02 DateOfBirth PIC X(8).
occurrences occurrences
Record Type Record Type
(Template) (Template)
(Structure) (Structure)
Example
REGNO NAME AGE
KA101 JYOTHI 19
KA102 ANIRUDH 20
KA103 SRIDHAR 18
Field-1 File Field-2 Field-3
Record-1
Record-2
Record-3
STUDENT
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
Record
Record
Buffers
Buffers
The record type/template/buffer of The record type/template/buffer of every every file used in a file used in a
program program must must be described in the FILE SECTION by be described in the FILE SECTION by
means of an FD (file description) entry. means of an FD (file description) entry.
The FD entry consists of the letters FD and an internal file The FD entry consists of the letters FD and an internal file
name. name.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(2).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(2).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
Implications of
Implications of
Buffers
Buffers
If your program processes more than one file you If your program processes more than one file you
will have to describe a record buffer for each file. will have to describe a record buffer for each file.
To process all the records in an INPUT file each To process all the records in an INPUT file each
record instance must be copied (read) from the file record instance must be copied (read) from the file
into the record buffer when required. into the record buffer when required.
To create an OUTPUT file containing data records To create an OUTPUT file containing data records
each record must be placed in the record buffer each record must be placed in the record buffer
and then transferred (written) to the file. and then transferred (written) to the file.
To transfer a record from an input file to an output To transfer a record from an input file to an output
file we will have to file we will have to
read the record into the input record buffer read the record into the input record buffer
transfer it to the output record buffer transfer it to the output record buffer
write the data to the output file from the write the data to the output file from the
output record buffer output record buffer
Description of a Record buffer for a file containing
Multiple record types
DATA DIVISION.
FILE SECTION.
FD TRANS-FILE.
01 INS-REC.
05 T-CODE PIC X.
05 REGNO PIC X(5).
05 NAME PIC A(15).
05 AGE PIC 9(2).
01 DEL-REC.
05 T-CODE PIC X.
05 REGNO PIC X(5).
01 UPD-REC.
05 T-CODE PIC X.
05 REGNO PIC X(5).
05 AGE PIC 9(2).
Record Buffer and its implications
Organization and
Organization and
Access
Access
Two important characteristics of files are Two important characteristics of files are
Data organization refers to the way the records of the file are Data organization refers to the way the records of the file are
organized on the backing storage device. organized on the backing storage device.
COBOL recognizes three main file organizations; COBOL recognizes three main file organizations;
Relative Relative - Relative record number based - Relative record number based
organization. organization.
The method of access refers to the way in which records are The method of access refers to the way in which records are
accessed. accessed.
A file with an organization of Indexed or Relative may A file with an organization of Indexed or Relative may
still have its records accessed sequentially. still have its records accessed sequentially.
But records in a file with an organization of Sequential But records in a file with an organization of Sequential
can not be accessed directly. can not be accessed directly.
Sequential
Sequential
Organization
Organization
The simplest COBOL file organization is Sequential. The simplest COBOL file organization is Sequential.
In a Sequential file the records are arranged serially, one In a Sequential file the records are arranged serially, one
after another, like cards in a dealing show. after another, like cards in a dealing show.
In a Sequential file the only way to access any particular In a Sequential file the only way to access any particular
record is to; record is to;
Start at the first record and read all the succeeding Start at the first record and read all the succeeding
records until you find the one you want or reach the end records until you find the one you want or reach the end
of the file. of the file.
The ordering of the records in a file has a significant impact The ordering of the records in a file has a significant impact
on the way in which it is processed and the processing that on the way in which it is processed and the processing that
can be done on it. can be done on it.
Sequential file organization
OPEN
READ
WRITE
REWRITE
CLOSE
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
COBOL file handling
COBOL file handling
Verbs
Verbs
OPEN OPEN
Before your program can access the data in an input file or Before your program can access the data in an input file or
place data in an output file you must make the file available to place data in an output file you must make the file available to
the program by OPENing it. the program by OPENing it.
READ READ
The READ copies a record occurrence/instance from the file The READ copies a record occurrence/instance from the file
and places it in the record buffer. and places it in the record buffer.
WRITE WRITE
The WRITE copies the record it finds in the record buffer to The WRITE copies the record it finds in the record buffer to
the file. the file.
CLOSE CLOSE
You must ensure that (before terminating) your program You must ensure that (before terminating) your program
closes all the files it has opened. Failure to do so may result in closes all the files it has opened. Failure to do so may result in
data not being written to the file or users being prevented data not being written to the file or users being prevented
from accessing the file. from accessing the file.
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
OPEN and CLOSE verb
OPEN and CLOSE verb
syntax
syntax
When you open a file you have to indicate to the When you open a file you have to indicate to the
system what how you want to use it (e.g. INPUT, system what how you want to use it (e.g. INPUT,
OUTPUT, EXTEND) so that the system can manage OUTPUT, EXTEND) so that the system can manage
the file correctly. the file correctly.
Opening a file does not transfer any data to the Opening a file does not transfer any data to the
record buffer, it simply provides access. record buffer, it simply provides access.
OPEN InternalFileName ...
INPUT
OUTPUT
EXTEND
'
'
OPEN verb
Syntax
OPEN {INPUT, OUTPUT, I-O, EXTEND} Filename-1 .
. .
OPEN MODE
STATEMENT INPUT OUTPUT I-O EXTEND
READ
WRITE
REWRITE
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
The READ verb
The READ verb
The InternalFilename specified must be a file that has The InternalFilename specified must be a file that has
been OPENed for INPUT. been OPENed for INPUT.
The NEXT RECORD clause is optional and generally The NEXT RECORD clause is optional and generally
not used. not used.
Using INTO Identifier clause causes the data to be Using INTO Identifier clause causes the data to be
read into the record buffer and then copied from there read into the record buffer and then copied from there
to the specified Identifier in one operation. to the specified Identifier in one operation.
When this option is used there will be two copies When this option is used there will be two copies
of the data. It is the equivalent of a READ of the data. It is the equivalent of a READ
followed by a MOVE. followed by a MOVE.
[ ]
[ ]
READ InternalFilename NEXT RECORD
INTO Identifier
AT END StatementBlock
END- READ
Working of the READ statement
STUD-REC
1 0 1 J
Y O T H I
2 5 B U
REGNO NAME AGE
1 3 A H N 2 B
EOF
1 2 I H A 2 B
0 R C A 0 U
0 N T Y 2 U
A
STUDENT
PERFORM UNTIL STUD-REC = HIGH-VALUES
READ STUDFILE
AT END MOVE HIGH-VALUES TO STUD-REC
END-READ
END-PERFORM.
Working of the READ statement
STUD-REC
1 0 1 J
Y
O T H I 2 5 B U
REGNO NAME AGE
1 3 A H N 2 B
EOF
1 2 I H A 2 B
0 R C A 0 U
0 N T Y 2 U
A
STUDENT
1 0 1 J
Y
O
T H
I 2 5 B U
PERFORM UNTIL STUD-REC = HIGH-VALUES
READ STUDFILE
AT END MOVE HIGH-VALUES TO STUD-REC
END-READ
END-PERFORM.
Working of the READ statement
1 0 1 J Y O T H I 2 5 B U
REGNO NAME AGE
1 3 A H N 2 B
EOF
0 R C A 0 U A
1 0 2 N I T H Y A 2 2 B U
1 0 2 N I T H Y A 2 2 B U
PERFORM UNTIL STUD-REC = HIGH-VALUES
READ STUDFILE
AT END MOVE HIGH-VALUES TO STUD-REC
END-READ
END-PERFORM.
STUD-REC
STUDENT
Working of the READ statement
1 0 1 J Y O T H I 2 5 B U
REGNO NAME AGE
EOF
1 0 3 R A C H A N 2 0 B U
1 0 2 N I T H Y A 2 2 B U
A
1 0 3 R A C H A N 2 0 B U A
PERFORM UNTIL STUD-REC = HIGH-VALUES
READ STUDFILE
AT END MOVE HIGH-VALUES TO STUD-REC
END-READ
END-PERFORM.
STUD-REC
STUDENT
Working of the READ statement
1 0 1 J
Y O
T H I 2 5 B U
REGNO NAME AGE
EOF
1 0 2 N I T H Y A 2 2 B U
1 0 3 R A C H A N 2 0 B U A
PERFORM UNTIL STUD-REC = HIGH-VALUES
READ STUDFILE
AT END MOVE HIGH-VALUES TO STUD-REC
END-READ
END-PERFORM.
STUD-REC
STUDENT
06/28/12 06/28/12 08:36 AM 08:36 AM Infosys Technologies Limited Infosys Technologies Limited
WRITE
WRITE
Syntax.
Syntax.
'
1
]
1
'
1
]
1
1
1
1
1
1
F F r r a a n n k k C C u u r r t t a a i i n n 9 9 3 3 3 3 4 4 5 5 6 6 7 7 L L M M 0 0 5 5 1 1
StudentID StudentName
Course.
StudentRecord
F F r r a a n n k k C C u u r r t t a a i i n n 9 9 3 3 3 3 4 4 5 5 6 6 7 7 L L M M 0 0 5 5 1 1
EOF
How the WRITE
How the WRITE
works
works
OPEN OUTPUT StudentFile.
MOVE "9334567Frank Curtain LM051" TO StudentDetails.
WRITE StudentDetails.
MOVE "9383715Thomas Healy LM068" TO StudentDetails.
WRITE StudentDetails.
CLOSE StudentFile.
STOP RUN.
OPEN OUTPUT StudentFile.
MOVE "9334567Frank Curtain LM051" TO StudentDetails.
WRITE StudentDetails.
MOVE "9383715Thomas Healy LM068" TO StudentDetails.
WRITE StudentDetails.
CLOSE StudentFile.
STOP RUN.
Students
Working of the WRITE statement
STUD-REC
1 0 1 J Y O T H I 2 5 B U
REGNO NAME AGE
EOF
1 0 1 J Y O T H
I
2 5 U
MOVE BU101JYOTHI 25 TO STUD-REC.
WRITE STUD-REC.
MOVE BU102NITHYA 22 TO STUD-REC.
WRITE STUD-REC.
B
STUDENT
Working of the WRITE statement
REGNO NAME AGE
EOF
1 0 2 N I T H Y A 2 2 B U
1 0 2 N I T H Y A 2 2 B U
1 0 1 J Y O T
H
I 2 5 U B
STUD-REC
STUDENT
MOVE BU101JYOTHI 25 TO STUD-REC.
WRITE STUD-REC.
MOVE BU102NITHYA 22 TO STUD-REC.
WRITE STUD-REC.
REWRITE verb
Syntax
CLOSE filename1