98509729 Cobol File Handling
Transcript of 98509729 Cobol File Handling
-
8/13/2019 98509729 Cobol File Handling
1/40
File Handling in
COBOL
-
8/13/2019 98509729 Cobol File Handling
2/40
Topics covered in the Session
(1) Introduction to File handling.
(2) File ORGANIATION and A!!"SS #ethods.
($) File handling ver%s.
-
8/13/2019 98509729 Cobol File Handling
3/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
COBOL is generally used inCOBOL is generally used insituations where the volume ofsituations where the volume of
data to be processed is large.data to be processed is large.
These systems are sometimesThese systems are sometimes
referred to as data intensivereferred to as data intensive
systems.systems.
-
8/13/2019 98509729 Cobol File Handling
4/40
Introduction to File processing
Field Field type and Field sie.
!ecord !ecord"#ie$ Fi%ed length records and&ariable length records.
File 'aster files$ Transaction files$ Fileorganiation and File access method.
/asic Ter#inologies
-
8/13/2019 98509729 Cobol File Handling
5/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
Fields.Fields. 0e use the ter# FI" to descri%e an ite# o,0e use the ter# FI" to descri%e an ite# o,
in,or#ation e are recording a%out an o%3ectin,or#ation e are recording a%out an o%3ect
(e.g. StudentNa#e4 ateO,/irth4 !ourse!ode).(e.g. StudentNa#e4 ateO,/irth4 !ourse!ode).
0e use the ter# R"!OR to descri%e the collection o,0e use the ter# R"!OR to descri%e the collection o,,ields hich record in,or#ation a%out an o%3ect,ields hich record in,or#ation a%out an o%3ect
(e.g. a StudentRecord is a collection o, ,ields(e.g. a StudentRecord is a collection o, ,ields
recording in,or#ation a%out a student).recording in,or#ation a%out a student).
0e use the ter# FI" to descri%e a collection o, one or0e use the ter# FI" to descri%e a collection o, one or#ore occurrences (instances) o, a record t-pe#ore occurrences (instances) o, a record t-pe(te#plate).(te#plate).
It is i#portant to distinguish %eteen the recordIt is i#portant to distinguish %eteen the recordoccurrence (i.e. the values o, a record) and the recordoccurrence (i.e. the values o, a record) and the recordt-pe (i.e. the structure o, the record).t-pe (i.e. the structure o, the record)."ver- record in a ,ile has a di,,erent value %ut the sa#e"ver- record in a ,ile has a di,,erent value %ut the sa#e
structure.structure.
-
8/13/2019 98509729 Cobol File Handling
6/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
Fields.Fields.
StudId StudName DateOfBirthStudId StudName DateOfBirth
9723456 COUGHLAN 1919619724567 !"AN 31121976953411# CO$$%" 2361964942345# O&B!I%N 31119799312#76 S'I(H 12121976
StudId StudName DateOfBirthStudId StudName DateOfBirth
9723456 COUGHLAN 1919619724567 !"AN 31121976953411# CO$$%" 2361964942345# O&B!I%N 31119799312#76 S'I(H 12121976
S(UD%N(SS(UD%N(S
DA(A DI)ISION*$IL% S%C(ION*
$D Stude+t$i,e*1 Stude+tDetai,-* 2 StudId .IC 9/70* 2 StudName .IC /#0* 2 DateOfBirth .IC /#0*
DA(A DI)ISION*$IL% S%C(ION*
$D Stude+t$i,e*1 Stude+tDetai,-* 2 StudId .IC 9/70* 2 StudName .IC /#0* 2 DateOfBirth .IC /#0*
occurrencesoccurrences
Record TypeRecord Type(Template)(Template)
(Structure)(Structure)
-
8/13/2019 98509729 Cobol File Handling
7/40
"5a#ple
!()*O *+'( +)(
6A1&1 78OT9I 1:
6A1&2 ANIR;9 2&6A1&$ SRI9AR 1
Field
-
8/13/2019 98509729 Cobol File Handling
8/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
BuffersBuffers
To process a file records are read from theTo process a file records are read from the
file into the computers memory one recordfile into the computers memory one recordat a time.at a time.
The computer uses the programmersThe computer uses the programmers
description of the record /i.e. the recorddescription of the record /i.e. the recordtemplate0 to set aside sufficient memory totemplate0 to set aside sufficient memory to
store one instance of the record.store one instance of the record.
'emory allocated for storing a record is'emory allocated for storing a record is
usually called a record bufferusually called a record buffer
The record buffer is the only connectionThe record buffer is the only connection
between the program and the records in thebetween the program and the records in the
file.file.
-
8/13/2019 98509729 Cobol File Handling
9/40
BuffersBuffers
IDENTIFICATION DIVISION.
etc.
ENVIRONMENT DIVISION.
etc.
DATA DIVISION.FIE SECTION.
!ro"ram!ro"ram
Record#u$$erRecord#u$$er
Declarat%onDeclarat%onST&DENTS
-1#2
Record Instance
-
8/13/2019 98509729 Cobol File Handling
10/40
escription o, a Record %u,,er ,or a ,ile containing
Single record t-pe
-+T+ -1&1#1O*.
F1L( #(CT1O*.
F- #T,-F1L(.
34 #T,-"!(C.
35 !()*O 61C 7/50.
35 *+'( 61C +/450.35 +)( 61C 8/90.
Record /u,,er and its i#plications
-
8/13/2019 98509729 Cobol File Handling
11/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
COBOLCOBOL
The record type:template:buffer ofThe record type:template:buffer of everyeveryfile used in afile used in aprogramprogram mustmustbe described in the F1L( #(CT1O* bybe described in the F1L( #(CT1O* by
means of an F- /file description0 entry.means of an F- /file description0 entry.
The F- entry consists of the letters F- and an internal fileThe F- entry consists of the letters F- and an internal file
name.name.
DA(A DI)ISION*$IL% S%C(ION*$D Stude+t$i,e*
1 Stude+tDetai,-* 2 Stude+tId .IC 9/70* 2 Stude+tName* 3 Sur+ame .IC /#0* 3 I+itia,- .IC * 2 DateOfBirth* 3 "OBirth .IC 9/20* 3 'OBirth .IC 9/20*
3 DOBirth .IC 9/20* 2 Cur-eCde .IC /40* 2 Gra+t .IC 9/40* 2 Ge+der .IC *
DA(A DI)ISION*$IL% S%C(ION*$D Stude+t$i,e*1 Stude+tDetai,-* 2 Stude+tId .IC 9/70* 2 Stude+tName* 3 Sur+ame .IC /#0* 3 I+itia,- .IC * 2 DateOfBirth* 3 "OBirth .IC 9/20* 3 'OBirth .IC 9/20*
3 DOBirth .IC 9/20* 2 Cur-eCde .IC /40* 2 Gra+t .IC 9/40* 2 Ge+der .IC *
-
8/13/2019 98509729 Cobol File Handling
12/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
;Buffers;Buffers
1f your program processes more than one file you1f 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 1*6,T file eachTo process all the records in an 1*6,T file each
record instance must be copied /read0 from the filerecord instance must be copied /read0 from the file
into the record buffer when re
-
8/13/2019 98509729 Cobol File Handling
13/40
escription o, a Record %u,,er ,or a ,ile containing+ultiple record t-pes
-+T+ -1&1#1O*.
F1L( #(CT1O*.
F- T!+*#"F1L(.
34 1*#"!(C. 35 T"CO-( 61C 7.
35 !()*O 61C 7/50.
35 *+'( 61C +/450.
35 +)( 61C 8/90.
34 -(L"!(C.
35 T"CO-( 61C 7.
35 !()*O 61C 7/50.
34 ,6-"!(C.
35 T"CO-( 61C 7.
35 !()*O 61C 7/50.
35 +)( 61C 8/90.
Record /u,,er and its i#plications
++
-
8/13/2019 98509729 Cobol File Handling
14/40
+ccess+ccess Two important characteristics of files areTwo important characteristics of files are
-+T+ O!)+*1=+T1O*-+T+ O!)+*1=+T1O*
'(T>O- OF +CC(##'(T>O- OF +CC(##
-ata organiation refers to the way the records of the file are-ata organiation refers to the way the records of the file are
organied on the bac?ing storage device.organied on the bac?ing storage device.
COBOL recognies three main file organiations@COBOL recognies three main file organiations@
#e
-
8/13/2019 98509729 Cobol File Handling
15/40
OrganiationOrganiation
The simplest COBOL file organiation is #e
-
8/13/2019 98509729 Cobol File Handling
16/40
Se=uential ,ile organi>ation
#implest and least fle%ible of all types of file
organiations.
Can only be accessed se
-
8/13/2019 98509729 Cobol File Handling
17/40
FI"
#(L(CT file"name +##1)* TO implementor"name
A O!)+*1=+T1O* 1# #(,(*T1+L
A +CC(## 'O-( 1# #(,(*T1+L
A F1L( #T+T,# 1# identifier .
ClCl
-
8/13/2019 98509729 Cobol File Handling
18/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
ST&DENTS
Clause.Clause.
The internal ,ile na#e used in the F entr- isThe internal ,ile na#e used in the F entr- isconnected to an e5ternal ,ile (on dis? or tape) %-connected to an e5ternal ,ile (on dis? or tape) %-
#eans o, the Select and Assign clause.#eans o, the Select and Assign clause.
%N)I!ON'%N( DI)ISION*IN.U(OU(.U( S%C(ION*$IL%CON(!OL* S%L%C( Stude+t$i,e
ASSIGN (O S(UD%N(S*
DA(A DI)ISION*$IL% S%C(ION*$D Stude+t$i,e*1 Stude+tDetai,-* 2 Stude+tId .IC 9/70*
2 Stude+tName* 3 Sur+ame .IC /#0* 3 I+itia,- .IC * 2 DateOfBirth* 3 "OBirth .IC 9/20* 3 'OBirth .IC 9/20* 3 DOBirth .IC 9/20* 2 Cur-eCde .IC /40*
2 Gra+t .IC 9/40* 2 Ge+der .IC *
%N)I!ON'%N( DI)ISION*IN.U(OU(.U( S%C(ION*$IL%CON(!OL* S%L%C( Stude+t$i,e
ASSIGN (O S(UD%N(S*
DA(A DI)ISION*$IL% S%C(ION*$D Stude+t$i,e*1 Stude+tDetai,-* 2 Stude+tId .IC 9/70* 2 Stude+tName*
3 Sur+ame .IC /#0* 3 I+itia,- .IC * 2 DateOfBirth* 3 "OBirth .IC 9/20* 3 'OBirth .IC 9/20* 3 DOBirth .IC 9/20* 2 Cur-eCde .IC /40* 2 Gra+t .IC 9/40* 2 Ge+der .IC *
-1#2
-
8/13/2019 98509729 Cobol File Handling
19/40
F entries ,or Fi5ed length records
F- file"name
A !(CO!- CO*T+1*# integer"4 C>+!+CT(!#
A BLOC2 CO*T+1*# integer"9 D !(CO!-#$
C>+!+CT(!# E
A L+B(L D !(CO!- 1#$ !(CO!-# +!( E
D #T+*-+!-$ O'1TT(- E
A -+T+ D !(CO!- 1#$ !(CO!-# +!( E identifier"4$
identifier"9$ . . .
-
8/13/2019 98509729 Cobol File Handling
20/40
F entries ,or @aria%le length records
F- file"name
A !(CO!- CO*T+1*# integer"4 TO integer"9
C>+!+CT(!#
A BLOC2 CO*T+1*# integer" TO integer"GC>+!+CT(!#
A L+B(L D !(CO!- 1#$ !(CO!-# +!( E
D #T+*-+!-$ O'1TT(- E
A -+T+ D !(CO!- 1#$ !(CO!-# +!( E identifier"4$
identifier"9$ . . .
-
8/13/2019 98509729 Cobol File Handling
21/40
File handling ver%s
O"N
R"A
0RIT"
R"0RIT"
!OS"
& b& b
-
8/13/2019 98509729 Cobol File Handling
22/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
&erbs&erbs O6(*O6(*
Before your program can access the data in an input file orBefore your program can access the data in an input file orplace data in an output file you must ma?e the file available toplace data in an output file you must ma?e the file available tothe program by O6(*ing it.the program by O6(*ing it.
!(+-!(+-The !(+- copies a record occurrence:instance from the fileThe !(+- copies a record occurrence:instance from the fileand places it in the record buffer.and places it in the record buffer.
H!1T(H!1T(The H!1T( copies the record it finds in the record buffer toThe H!1T( copies the record it finds in the record buffer tothe file.the file.
CLO#(CLO#(Iou must ensure that /before terminating0 your programIou must ensure that /before terminating0 your programcloses all the files it has opened. Failure to do so may result incloses all the files it has opened. Failure to do so may result in
data not being written to the file or users being preventeddata not being written to the file or users being preventedfrom accessing the file.from accessing the file.
tt
-
8/13/2019 98509729 Cobol File Handling
23/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
synta%synta%
Hhen you open a file you have to indicate to theHhen you open a file you have to indicate to the
system what how you want to use it /e.g. 1*6,T$system what how you want to use it /e.g. 1*6,T$
O,T6,T$ (7T(*-0 so that the system can manageO,T6,T$ (7T(*-0 so that the system can manage
the file correctly.the file correctly.
Opening a file does not transfer any data to theOpening a file does not transfer any data to the
record buffer$ it simply provides access.record buffer$ it simply provides access.
O"N InternalFileNa#e ...
IN;T
O;T;T
"BT"N1
-
8/13/2019 98509729 Cobol File Handling
24/40
O"N ver% S-nta5
O"N CIN;T4 O;T;T4 I
-
8/13/2019 98509729 Cobol File Handling
25/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
The !(+- verbThe !(+- verb
Once the s-ste# has opened a ,ile and #ade itOnce the s-ste# has opened a ,ile and #ade it
availa%le to the progra# it is the progra##ersavaila%le to the progra# it is the progra##ers
responsi%ilit- to process it correctl-.responsi%ilit- to process it correctl-.
Re#e#%er4 the ,ile record %u,,er is our onl-Re#e#%er4 the ,ile record %u,,er is our onl-
connection ith the ,ile and it is onl- a%le toconnection ith the ,ile and it is onl- a%le to
store a single record at a ti#e.store a single record at a ti#e.
To process all the records in the ,ile e have toTo process all the records in the ,ile e have to
trans,er the#4 one record at a ti#e4 ,ro# the ,iletrans,er the#4 one record at a ti#e4 ,ro# the ,ileto the %u,,er.to the %u,,er.
!O/O provides the R"A ver% ,or this!O/O provides the R"A ver% ,or this
purpose.purpose.
synta%synta%
-
8/13/2019 98509729 Cobol File Handling
26/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
synta%synta%
The 1nternalFilename specified must be a file that hasThe 1nternalFilename specified must be a file that has
been O6(*ed for 1*6,T.been O6(*ed for 1*6,T.
The *(7T !(CO!- clause is optional and generallyThe *(7T !(CO!- clause is optional and generally
not used.not used.
,sing 1*TO 1dentifier clause causes the data to be,sing 1*TO 1dentifier clause causes the data to be
read into the record buffer and then copied from thereread into the record buffer and then copied from there
to the specified 1dentifier in one operation.to the specified 1dentifier in one operation.
Hhen this option is used there will be two copiesHhen this option is used there will be two copies
of the data. 1t is the e
-
8/13/2019 98509729 Cobol File Handling
27/40
0or?ing o, the R"A state#ent#T,-"!(C
1 & 1 7 8 O T 9 I 2 E/ ;
!()*O *+'( +)(
1 $ A 9 N 2/
(OF
1 2 I 9 A 2/
& R ! A &;
& N T 8 2;
A
#T,-(*T
"RFOR+ ;NTI ST;
-
8/13/2019 98509729 Cobol File Handling
28/40
-
8/13/2019 98509729 Cobol File Handling
29/40
-
8/13/2019 98509729 Cobol File Handling
30/40
-
8/13/2019 98509729 Cobol File Handling
31/40
#ynta%#ynta%
-
8/13/2019 98509729 Cobol File Handling
32/40
&'212&'212&*$' A+&*$' A+ In,os-s Technologies i#itedIn,os-s Technologies i#ited
#ynta%.#ynta%.
To 0RIT" data to a ,ile #ove the dataTo 0RIT" data to a ,ile #ove the data
to the record %u,,er (declared in the Fto the record %u,,er (declared in the F
entr-) and then 0RIT"entr-) and then 0RIT"the contents o,the contents o,
record %u,,er to the ,ile.record %u,,er to the ,ile.
0RIT"
A1@AN!ING
AdvanceNu#
+ne#onicNa#e
AG"
RecordNa#e FRO+ Identi,ier
/"FOR"
AFT"R
IN"
IN"S
wor?swor?s
-
8/13/2019 98509729 Cobol File Handling
33/40
FF rr aa nn '' CC uu rr tt aa %% nn ** ++ ,, -- MM ++ //
StudentID StudentName Course.
StudentRecord
FF rr aa nn '' CC uu rr tt aa %% nn ** ++ ,, -- MM ++ //
EOF
wor?swor?s
O.%N OU(.U( Stude+t$i,e*'O)% 9334567$ra+ Curtai+ L'51 (O Stude+tDetai,-*
8!I(% Stude+tDetai,-*
'O)% 93#3715(hma- Hea, L'6# (O Stude+tDetai,-*
8!I(% Stude+tDetai,-*CLOS% Stude+t$i,e*
S(O. !UN*
O.%N OU(.U( Stude+t$i,e*
'O)% 9334567$ra+ Curtai+ L'51 (O Stude+tDetai,-*
8!I(% Stude+tDetai,-*
'O)% 93#3715(hma- Hea, L'6# (O Stude+tDetai,-*8!I(% Stude+tDetai,-*
CLOS% Stude+t$i,e*S(O. !UN*
Students
-
8/13/2019 98509729 Cobol File Handling
34/40
0or?ing o, the 0RIT" state#ent
#T,-"!(C
1 & 1 7 8 O T 9 I 2 E/ ;
!()*O *+'( +)(
(OF
1 & 1 7 8 O T 9 I 2 E;
+O@" /;1&178OT9I 2EH TO ST;
-
8/13/2019 98509729 Cobol File Handling
35/40
0or?ing o, the 0RIT" state#ent
!()*O *+'( +)(
(OF
1 & 2 N I T 9 8 A 2 2/ ;
1 & 2 N I T 9 8 A 2 2/ ;
1 & 1 7 8 O T 9 I 2 E;/
#T,-"!(C
#T,-(*T
+O@" /;1&178OT9I 2EH TO ST;
-
8/13/2019 98509729 Cobol File Handling
36/40
R"0RIT" ver%
R"0RIT" is used to update an e5isting record inthe ,ile
S-nta5
!(H!1T( record"name A F!O' identifier"4
*oteJ
The !(H!1T( statement can only be used if the file is
opened in the 1"O mode and its e%ecution must bepreceded by the successful !(+- statement on the file.
The !(H!1T( statement replaces last read record
-
8/13/2019 98509729 Cobol File Handling
37/40
!OS" ver%
S-nta5
CLO#( filename4
!eleases the named files from the program.
1f a file is stored on a magnetic tape$ after the
e%ecution
of the CLO#( statement the tape is rewound.
1s optional for COBOL" K5.
-
8/13/2019 98509729 Cobol File Handling
38/40
Se=uential ,iles
-
8/13/2019 98509729 Cobol File Handling
39/40
+ny+nyuestions uestions
-
8/13/2019 98509729 Cobol File Handling
40/40
Than? youThan? you