Cobol File Handling
Cobol File Handling
COBOL
Topics covered in the Session
(1) Introduction to File handling.
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
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.
[ ORGANIZATION IS SEQUENTIAL ]
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.
OPEN MODE
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
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
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
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.
CLOSE filename1
Dis-advantages
Slow - when the hit rate is low.
Complicated to change (insert, delete).
Any
Questions ????
Thank you