0% found this document useful (0 votes)
193 views

Cobol File Handling

The document discusses file handling in COBOL. It covers file organization and access methods, and file handling verbs. It defines key terms like field, record, and file. It describes sequential, relative, and indexed file organizations and how records are accessed sequentially or directly. It also explains how record buffers are used to transfer records between main memory and files on disk.

Uploaded by

Santanu Bhadra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

Cobol File Handling

The document discusses file handling in COBOL. It covers file organization and access methods, and file handling verbs. It defines key terms like field, record, and file. It describes sequential, relative, and indexed file organizations and how records are accessed sequentially or directly. It also explains how record buffers are used to transfer records between main memory and files on disk.

Uploaded by

Santanu Bhadra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

File Handling in

COBOL
Topics covered in the Session
(1) Introduction to File handling.

(2) File ORGANIZATION and ACCESS methods.

(3) File handling verbs.


 COBOL is generally used in
situations where the volume of
data to be processed is large.

 These systems are sometimes


referred to as “data intensive”
systems.

11/29/21 12:44 PM Infosys Technologies Limited


Introduction to File
processing
Basic Terminologies

 Field Field type and Field size.

 Record Record-Size, Fixed length records and


Variable length records.

 File Master files, Transaction files, File


organization and File access method.
Fields.
 We use the term FIELD to describe an item of
information we are recording about an object
(e.g. StudentName, DateOfBirth, CourseCode).
 We use the term RECORD to describe the collection
of fields which record information about an object
(e.g. a StudentRecord is a collection of fields
recording information about a student).
 We use the term FILE to describe a collection of one
or more occurrences (instances) of a record type
(template).
 It is important to distinguish between the record
occurrence (i.e. the values of a record) and the record
type (i.e. the structure of the record).
Every record in a file has a different value but the
11/29/21 12:44same
PM structure. Infosys Technologies Limited
Fields.
STUDENTS
StudId
StudId StudName
StudName DateOfBirth
DateOfBirth
9723456
9723456 COUGHLAN
COUGHLAN 10091961
10091961
9724567 RYAN
9724567 RYAN 31121976
31121976
9534118
9534118 COFFEY
COFFEY 23061964
23061964 occurrences
9423458 O'BRIEN
9423458 O'BRIEN 03111979
03111979
9312876
9312876 SMITH
SMITH 12121976
12121976

DATA Record Type


DATA DIVISION.
DIVISION.
FILE SECTION. (Template)
FILE SECTION.
FD
FD StudentFile.
StudentFile. (Structure)
01 StudentDetails.
01 StudentDetails.
02
02 StudId
StudId PIC
PIC 9(7).
9(7).
02 StudName
02 StudName PIC X(8).
PIC X(8).
02
02 DateOfBirth PIC X(8).
DateOfBirth PIC X(8).

11/29/21 12:44 PM Infosys Technologies Limited


Example
Field-1 File Field-2 Field-3

STUDENT
REGNO NAME AGE
KA101 JYOTHI 19 Record-1
KA102 ANIRUDH 20 Record-2
KA103 SRIDHAR 18 Record-3
Buffers
 To process a file records are read from the file
into the computer’s memory one record at a
time.
 The computer uses the programmers
description of the record (i.e. the record
template) to set aside sufficient memory to
store one instance of the record.
 Memory allocated for storing a record is
usually called a “record buffer”
 The record buffer is the only connection
between the program and the records in the
file.
11/29/21 12:44 PM Infosys Technologies Limited
Buffers
Program
IDENTIFICATION DIVISION.
etc.
ENVIRONMENT DIVISION.
etc.
DATA DIVISION.
DISK FILE SECTION.
Record Instance
STUDENTS RecordBuffer
Declaration
Record Buffer and its implications

Description of a Record buffer for a file containing


Single record type

DATA DIVISION.
FILE SECTION.
FD STUDFILE.
01 STUD-REC.
05 REGNO PIC X(5).
05 NAME PIC A(15).
05 AGE PIC 9(2).
Describing the record buffer in COBOL
DATA
DATA DIVISION.
DIVISION.
FILE
FILE SECTION.
SECTION.
FD StudentFile.
FD StudentDetails.
StudentFile.
01
01 02
StudentDetails.
02 StudentId
StudentId PIC
PIC 9(7).
9(7).
02 StudentName.
02 03StudentName.
03 Surname
Surname PIC
PIC X(8).
X(8).
03 Initials
03 Initials PIC XX.
PIC XX.
02 DateOfBirth.
02 03DateOfBirth.
03 YOBirth
YOBirth PIC
PIC 9(2).
9(2).
03 MOBirth
03 DOBirth
MOBirth PIC 9(2).
PIC 9(2).
9(2).
03
03 DOBirth PIC
PIC X(4).
9(2).
02 CourseCode
02 Grant
CourseCode PIC
PIC 9(4).
X(4).
02
02 Gender
Grant PIC
PIC X.
9(4).
02
02 Gender PIC
PIC X.

 The record type/template/buffer of every file used in a program


must be described in the FILE SECTION by means of an FD (file
description) entry.
 The FD entry consists of the letters FD and an internal file name.

11/29/21 12:44 PM Infosys Technologies Limited


‘Buffers’
 If your program processes more than one file you
will have to describe a record buffer for each file.
 To process all the records in an INPUT file each
record instance must be copied (read) from the file
into the record buffer when required.
 To create an OUTPUT file containing data records
each record must be placed in the record buffer
and then transferred (written) to the file.
 To transfer a record from an input file to an
output file we will have to
 read the record into the input record buffer

 transfer it to the output record buffer

 write the data to the output file from the

output record buffer


11/29/21 12:44 PM Infosys Technologies Limited
Access
 Two important characteristics of files are
 DATA ORGANIZATION
 METHOD OF ACCESS
 Data organization refers to the way the records of the file are
organized on the backing storage device.
COBOL recognizes three main file organizations;
 Sequential - Records organized serially.
 Relative - Relative record number based
organization.
 Indexed - Index based organization.
 The method of access refers to the way in which records are
accessed.
 A file with an organization of Indexed or Relative may
still have its records accessed sequentially.
 But records in a file with an organization of Sequential
can not be accessed directly.
Organization
 The simplest COBOL file organization is Sequential.
 In a Sequential file the records are arranged serially, one
after another, like cards in a dealing show.
 In a Sequential file the only way to access any particular
record is to;
Start at the first record and read all the succeeding
records until you find the one you want or reach the end
of the file.
 Sequential files may be
Ordered
or
Unordered (these should be called Serial files)
 The ordering of the records in a file has a significant impact
on the way in which it is processed and the processing that
can be done on it.
Sequential file organization

 Simplest and least flexible of all types of file


organizations.

 Can only be accessed sequentially.

 Records can be only added to the end of the file.

 Does not provide means to insert or delete records.

 Most storage efficient.


FILE-CONTROL paragraph for sequential
files
SELECT file-name ASSIGN TO implementor-name

[ ORGANIZATION IS SEQUENTIAL ]

[ ACCESS MODE IS SEQUENTIAL]

[ FILE STATUS IS identifier ].


Clause.
ENVIRONMENT
ENVIRONMENT DIVISION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT SECTION.
SECTION.
FILE-CONTROL.
FILE-CONTROL.
SELECT
SELECT StudentFile
StudentFile
ASSIGN
ASSIGN TO
TO “STUDENTS”.
“STUDENTS”.
DATA
DATA DIVISION.
DIVISION.
FILE
FILE SECTION.
SECTION.
FD StudentFile.
FD StudentDetails.
StudentFile.
DISK 01
01 02
StudentDetails.
02 StudentId
StudentId PIC
PIC 9(7).
9(7).
02 StudentName.
02 03StudentName.
03 Surname
Surname PIC
PIC X(8).
X(8).
STUDENTS 03 Initials PIC XX.
02 03 Initials
DateOfBirth. PIC XX.
02 03DateOfBirth.
YOBirth PIC 9(2).
03
03 YOBirth
MOBirth PIC
PIC 9(2).
9(2).
03 DOBirth
03 MOBirth PIC 9(2).
PIC 9(2).
02 03 DOBirth
CourseCode PIC X(4).
PIC 9(2).
02 Grant
02 CourseCode PIC 9(4).
PIC X(4).
02 Gender
02 Grant PIC X.
PIC 9(4).
02 Gender PIC X.

 The internal file name used in the FD entry is connected to an external


file (on disk or tape) by means of the Select and Assign clause.

11/29/21 12:44 PM Infosys Technologies Limited


File handling verbs
 OPEN

 READ

 WRITE

 REWRITE

 CLOSE
Verbs
 OPEN
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
the program by OPENing it.
 READ
The READ copies a record occurrence/instance from the file
and places it in the record buffer.
 WRITE
The WRITE copies the record it finds in the record buffer to
the file.
 CLOSE
You must ensure that (before terminating) your program
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
from accessing the file.

11/29/21 12:44 PM Infosys Technologies Limited


syntax
  INPUT  
  
OPEN   OUTPUT  InternalFileName ...
  EXTEND  
   

 When you open a file you have to indicate to the system


what how you want to use it (e.g. INPUT, OUTPUT,
EXTEND) so that the system can manage the file correctly.

 Opening a file does not transfer any data to the record


buffer, it simply provides access.

11/29/21 12:44 PM Infosys Technologies Limited


OPEN verb
 Syntax

OPEN {INPUT, OUTPUT, I-O, EXTEND} Filename-1 . . .

OPEN MODE

STATEMENT INPUT OUTPUT I-O EXTEND

READ

WRITE

REWRITE
The READ verb
 Once the system has opened a file and made it
available to the program it is the programmers
responsibility to process it correctly.
 Remember, the file record buffer is our only
connection with the file and it is only able to
store a single record at a time.
 To process all the records in the file we have to
transfer them, one record at a time, from the file
to the buffer.
 COBOL provides the READ verb for this
11/29/21 12:44 PM Infosys Technologies Limited
purpose.
syntax
READ InternalFilename  NEXT RECORD
 INTO Identifier
AT END StatementBlock
END - READ
 The InternalFilename specified must be a file that has
been OPENed for INPUT.
 The NEXT RECORD clause is optional and generally
not used.
 Using INTO Identifier clause causes the data to be
read into the record buffer and then copied from
there to the specified Identifier in one operation.
 When this option is used there will be two copies

of the data. It is the equivalent of a READ


followed by a MOVE.

11/29/21 12:44 PM Infosys Technologies Limited


Working of the READ
statement
STUD-REC
REGNO NAME AGE

STUDENT
B U 1 0 1 J YO TH I 2 5
B U 1 0 2 N I T H Y A 2 2
B U 1 0 3 R A C H A N A 2 0

EOF
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
REGNO NAME AGE
B U 1 0 1 J Y O TH I 2 5
STUDENT
B U 1 0 1 J Y O T H I 2 5
B U 1 0 2 N I T H Y A 2 2
B U 1 0 3 R A C H A N A 2 0

EOF
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
REGNO NAME AGE
B U 1 0 2 N I T H Y A 2 2
STUDENT
B U 1 0 1 J Y O T H I 2 5
B U 1 0 2 N I T H Y A 2 2
B U 1 0 3 R A C H A N A 2 0

EOF
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
REGNO NAME AGE
B U 1 0 3 R A C H A N A 2 0
STUDENT
B U 1 0 1 J Y O T H I 2 5
B U 1 0 2 N I T H Y A 2 2
B U 1 0 3 R A C H A N A 2 0

EOF
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
REGNO NAME AGE
                     

STUDENT
B U 1 0 1 J YO T H I 2 5
B U 1 0 2 N I T H Y A 2 2
B U 1 0 3 R A C H A N A 2 0
EOF
PERFORM UNTIL STUD-REC = HIGH-VALUES
READ STUDFILE
AT END MOVE HIGH-VALUES TO STUD-REC
END-READ
END-PERFORM.
Syntax.
WRITE RecordName  FROM Identifier
   LINE   
  AdvanceNum   
  BEFORE    LINES  
 ADVANCING  MnemonicName 
 AFTER   PAGE 
  
   

 To WRITE data to a file move the data to


the record buffer (declared in the FD
entry) and then WRITE the contents of
record buffer to the file.
11/29/21 12:44 PM Infosys Technologies Limited
works
OPEN
OPEN OUTPUT
OUTPUT StudentFile.
StudentFile.
MOVE
MOVE "9334567Frank Curtain
"9334567Frank Curtain LM051"
LM051" TO
TO StudentDetails.
StudentDetails.
WRITE StudentDetails.
WRITE StudentDetails.
MOVE
MOVE "9383715Thomas
"9383715Thomas Healy
Healy LM068"
LM068" TO
TO StudentDetails.
StudentDetails.
WRITE StudentDetails.
WRITE StudentDetails.
CLOSE
CLOSE StudentFile.
StudentFile.
STOP RUN.
STOP RUN.

StudentRecord
StudentID StudentName Course.
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1

Students
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
EO
F
Working of the WRITE statement
MOVE “BU101JYOTHI 25” TO STUD-REC.
WRITE STUD-REC.
MOVE “BU102NITHYA 22” TO STUD-REC.
WRITE STUD-REC.

STUD-REC
REGNO NAME AGE
B U 1 0 1 J Y O T H I 2 5

STUDENT
B U 1 0 1 J Y O T H I 2 5

EOF
Working of the WRITE statement
MOVE “BU101JYOTHI 25” TO STUD-REC.
WRITE STUD-REC.
MOVE “BU102NITHYA 22” TO STUD-REC.
WRITE STUD-REC.

STUD-REC
REGNO NAME AGE
B U 1 0 2 N I T H Y A 2 2

STUDENT
B U 1 0 1 J Y O T H I 2 5
B U 1 0 2 N I T H Y A 2 2
EOF
REWRITE verb
•REWRITE is used to update an existing record in
the file
Syntax

REWRITE record-name [ FROM identifier-1 ]

Note:
•The REWRITE statement can only be used if the file is
opened in the I-O mode and its execution must be
preceded by the successful READ statement on the file.

•The REWRITE statement replaces last read record


CLOSE verb
 Syntax

CLOSE filename1

 Releases the named files from the program.

 If a file is stored on a magnetic tape, after the


execution
of the CLOSE statement the tape is rewound.

 Is optional for COBOL- 85.


Sequential files - A Final Look
Advantages
Fast - when the hit rate is high.
Most storage efficient.
Simple organization.

Dis-advantages
Slow - when the hit rate is low.
Complicated to change (insert, delete).
Any
Questions ????
Thank you

You might also like