SlideShare une entreprise Scribd logo
1  sur  113
Objectives of the Session
(1) Introduction to File handling.
(2) File ORGANIZATION and ACCESS methods.
(3) File handling verbs.
(4) Sequential File processing.
(5) Demonstration of sample programs.
FILES
 COBOL is generally used in situations
where the volume of data to be
processed is large.
 TO STORE LARGE VOLUMES OF
DATA AND RETRIEVE IT LATER
IN PROGRAMMING
 To store Values(data) permanently
Sequential files
 Records can be accessed in the order of
their appearance
 Magnetic tape file  sequential
 Disk file  sequential, random etc
Introduction to File processing
Field - Field type and Field size.
FIELD to describe an item of information we are recording about an
object (e.g. StudentName, DateOfBirth, CourseCode).
 Record Record-Size, Fixed length records and
Variable length records.
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).

Basic Terminologies
01 StudentRecord.
02 regno pic 9999.
02 name pic x(20).
02 address pic x(20)
Files.
File Master files, Transaction files, File
organization and File access method.
FILE to describe a collection of one or more
occurrences (instances) of a record type
(template).
 Record occurrence (i.e. the values of a
record)
 Record type (i.e. the structure of the
record).
Every record in a file has a different value
but the same structure.
Files, Records, Fields.
StudId StudName DateOfBirth
9723456 COUGHLAN 10091961
9724567 RYAN 31121976
9534118 COFFEY 23061964
9423458 O'BRIEN 03111979
9312876 SMITH 12121976
STUDENTS
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudId PIC 9(7).
02 StudName PIC X(8).
02 DateOfBirth PIC X(8).
occurrences
Record Type
(Template)
(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
FILE CHARACTERISTICS
The task of file handling is the responsibility of
the system software known as IOCS (Input-
Output control system).
Programmer  only specify file characteristics
Record Size:
The size of the records may be chosen by
the programmer.
Records contain fixed and/or variables size
records
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudId PIC 9(7).
02 StudName PIC X(8).
02 DateOfBirth PIC X(8).
Block Size:
•group a number of consecutive records to
form a block
•The number of records in a block is often called
“blocking factors”.
•The actual handling of blocks is transparent to
the programmer  specify no. of records in a
block
•Advantage  saving of input output time
Memory can be saved (inter record gap)
•This memory space is known as the buffer.
Buffers:
 In Modern computers  I-O operations
independent of the CPU by means of the
hardware  data channel.
 Enable overlapping of I-O with other CPU
operation
 To take advantage of this situation --> more
than one buffer for a file
Organization and 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.
METHOD OF ACCESS
 The method of access refers to the way in which
records are accessed. (sequential and random)
– 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.
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 hardware-name
[ RESERVE integer-1 { AREA/AREAS}
[ ORGANIZATION IS SEQUENTIAL ]
[ ACCESS MODE IS SEQUENTIAL]
[ FILE STATUS IS identifier ].
ORGANIZATION/ACCESS :
•Optional
•Default sequential organization and sequential access
RESERVE:
• The number of buffers used for the file.
•RESERVE 1 AREA means only one buffer is to be used
•Optional  default two buffers
FILE STATUS: VALUES-alphanumeric values.
00:-Successful completion
10:- At end condition
30:- Permanent error condition
34:-Boundary violation
STUDENTS
The Select and Assign Clause.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO DISK
organization is sequential.
DISK
FD entries for Fixed length records
FD file-name
[ RECORD CONTAINS integer-1 CHARACTERS ]
[ BLOCK CONTAINS integer-2 { RECORDS, CHARACTERS }]
[ LABEL { RECORD IS, RECORDS ARE } { STANDARD, OMITTED }]
[VALUE OF implementor-name-1 IS {dataname-2, literal-2]
[ DATA { RECORD IS, RECORDS ARE } identifier-1,
identifier-2, . . . ].
BLOCK CONTAINS CLAUSE:
•Integer –1 of the BLOCK CONTAINS clause specifies the
block size either in terms of records or in terms of
characters.
•For example, BLOCK CONTAINS 50 RECORDS
means that there are 50 records in the block.
•If omitted -- > one record per block
RECORDS CONTAINS CLAUSE:
•This clause specifies the records size.
•Integer-1 specifies the number of characters in a record.
•The RECORD CONTAINS clause is used for
documentary purposes only.
LABEL RECORD CLAUSE:
This clause specifies whether or not the standard header
and trailer labels should be present in the magnetic-tape
files.
Label Records clause for disk files should be specified
with Standard
VALUE OF CLAUSE:
The VALUE OF clause is entirely implementation-
dependent. In most compilers this clause is used to specify
a file title. The clause in such cases has the form.
DATA RECORD CLAUSE:
•This clause documents the record names defined for
the file.
•For example, DATA RECORDS ARE REC-1, REC-
2, REC-3 means that there are three different record
descriptions following the FD entry in which this DATA
RECORDS clause is used.
Example of file-description entries:
FD FILE – A
RECORD CONTAINS 130 CHARACTERS
BLOCK CONTAINS 20 RECORDS
DATA RECORD IS FIRST-RECORD
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS “MY-LIFE.txt”.
01 FIRST-RECORD PIC X(130).
Record Buffers
IDENTIFICATION DIVISION.
etc.
ENVIRONMENT DIVISION.
etc.
DATA DIVISION.
FILE SECTION.
Program
RecordBuffer
Declaration
STUDENTS
DISK Record Instance
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.
IDENTIFICATION DIVISION.
PROGRAM-ID. FIRSTFILE1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OUTFILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION
FD OUTFILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "OUT.TXT".
01 OUTREC.
02 REG PIC 9(5).
02 NAM PIC X(20).
02 ADDR PIC X(40).
WORKING-STORAGE SECTION.
77 A PIC 999.
PROCEDURE DIVISION.
MAINPARA.
OPEN OUTPUT OUTFILE.
ACCEPT REG.
ACCEPT NAM.
ACCEPT ADDR.
WRITE OUTREC.
CLOSE OUTFILE.
OPEN INPUT OUTFILE.
READ OUTFILE.
CLOSE OUTFILE.
STOP RUN.
FD OUTFILE
01 OUTREC.
02 REG PIC 9(5).
02 NAM PIC X(20).
02 ADDR PIC X(40).
MAINPARA.
DISPLAY "ENTER NUMBER OF RECORDS".
ACCEPT N.
OPEN OUTPUT OUTFILE.
PERFORM ACCEPT-PARA N TIMES.
CLOSE OUTFILE.
OPEN INPUT OUTFILE.
DISPLAY " THE RECORD DISPLAYED AFTER READING".
PERFORM DISPLAY-PARA N TIMES.
CLOSE OUTFILE.
STOP RUN.
ACCEPT-PARA.
DISPLAY "ENTER THE VALUE FOR EMP RECORD".
ACCEPT REG.
ACCEPT NAM.
ACCEPT ADDR.
WRITE OUTREC.
DISPLAY-PARA.
READ OUTFILE.
DISPLAY OUTREC.
File handling verbs
 OPEN
 READ
 WRITE
 REWRITE
 CLOSE
COBOL file handling Verbs
 OPEN
Before your program can access the data in an input file or
place data in an output file  program OPEN 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.
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
 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
purpose.
READ verb syntax
 The InternalFilename specified must be a file that
has been OPENed for INPUT.
 The NEXT RECORD clause is optional and
generally not used.
 
 
lockStatementBENDAT
IdentifierINTO
RECORDNEXTlenameInternalFiREAD
 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.
Working of the READ statement
STUD-REC
1 0 1 J Y O T H I 2 5B U
REGNO NAME AGE
1 3 A H N 2B
EOF
1 2 I H A 2B
0 R C A 0U
0 N T Y 2U
A
STUDENT
PERFORM P1 UNTIL LAST = -1
. . . .
P1.
READ STUDFILE AT END MOVE -1 TO LAST
Working of the READ statement
STUD-REC
1 0 1 J Y O T H I 2 5B U
REGNO NAME AGE
1 3 A H N 2B
EOF
1 2 I H A 2B
0 R C A 0U
0 N T Y 2U
A
STUDENT
1 0 1 J Y O T H I 2 5B U
PERFORM P1 UNTIL LAST = -1
. . . .
P1.
READ STUDFILE
AT END MOVE -1 TO LAST
Working of the READ statement
1 0 1 J Y O T H I 2 5B U
REGNO NAME AGE
1 3 A H N 2B
EOF
0 R C A 0U A
1 0 2 N I T H Y A 2 2B U
1 0 2 N I T H Y A 2 2B U
PERFORM P1 UNTIL LAST = -1
. . . .
P1.
READ STUDFILE
AT END MOVE -1 TO LAST
STUD-REC
STUDENT
Working of the READ statement
1 0 1 J Y O T H I 2 5B U
REGNO NAME AGE
EOF
1 0 3 R A C H A N 2 0B U
1 0 2 N I T H Y A 2 2B U
A
1 0 3 R A C H A N 2 0B U A
PERFORM P1 UNTIL LAST = -1
. . . .
P1.
READ STUDFILE
AT END MOVE -1 TO LAST
STUD-REC
STUDENT
Working of the READ statement
1 0 1 J Y O T H I 2 5B U
REGNO NAME AGE
EOF
J J J J J J J J J J JJ J
1 0 2 N I T H Y A 2 2B U
J
1 0 3 R A C H A N 2 0B U A
J J J J JJ JJ
PERFORM P1 UNTIL LAST = -1
. . . .
P1.
READ STUDFILE
AT END MOVE -1 TO LAST
STUD-REC
STUDENT
WRITE Syntax.
 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.
 WRITE
ADVANCING
AdvanceNum
MnemonicName
PAGE
RecordName FROM Identifier
BEFORE
AFTER
LINE
LINES










































F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1
StudentID StudentName Course.
StudentRecord
F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1
EOF
How the WRITE works
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 5B U
REGNO NAME AGE
EOF
1 0 1 J Y O T H I 2 5U
MOVE "BU101" TO REGNO
MOVE "JYOTHI" TO NAME
MOVE 25 TO AGE.
WRITE STUD-REC.
.
B
STUDENT
01 STUD-REC
02 REGNO PIC X(5).
02 NAME PIC X(15).
02 AGE PIC 99.
Working of the WRITE statement
REGNO NAME AGE
EOF
1 0 2 N I T H Y A 2 2B U
1 0 2 N I T H Y A 2 2B U
1 0 1 J Y O T H I 2 5UB
STUD-REC
STUDENT
MOVE “BU101JYOTHI 25” TO STUD-REC.
WRITE STUD-REC.
MOVE “BU102NITHYA 22” TO STUD-REC.
WRITE STUD-REC.
 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
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OUTFILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT INFILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION
FD INFILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "IN.TXT".
01 INREC.
02 REGNO PIC 9(5).
02 NAME PIC X(20).
02 ADDRESS PIC X(40).
FD OUTFILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "FILEOUT.TXT".
01 OUTREC.
02 REG PIC 9(5).
02 FILLER PIC X(3).
02 NAM PIC X(20).
02 FILLER PIC X(3).
02 ADDR PIC X(40).
WORKING-STORAGE SECTION.
77 LAST PIC S9 VALUE 0.
PROCEDURE DIVISION.
MAINPARA.
OPEN INPUT INFILE.
OPEN OUTPUT OUTFILE.
PERFORM READ-WRITE-PARA UNTIL LAST = -1.
CLOSE INFILE, OUTFILE.
STOP RUN.
READ-WRITE-PARA.
READ INFILE AT END MOVE -1 TO LAST.
IF LAST NOT = -1
MOVE REGNO TO REG
MOVE NAME TO NAM
MOVE ADDRESS TO ADDR
DISPLAY OUTREC
WRITE OUTREC.
To search a record in a file
- open the file
- accept the value of record to find
- perform the readpara until last = -1
-
- in readpara
- read each record at end move -1 to last
- after reading check whether that record is the
last record
WORKING-STORAGE SECTION.
77 SFLAG PIC 99 VALUE 0.
77 SNAME PIC X(20).
77 LAST PIC S9 VALUE 0.
PROCEDURE DIVISION.
MAINPARA.
OPEN INPUT INFILE.
DISPLAY "ENTER THE NAME TO SEARCH".
ACCEPT SNAME.
PERFORM READ-MODIFY-PARA UNTIL LAST = -1.
IF SFLAG = 0
DISPLAY "NO RECORDS PRESENT".
CLOSE INFILE.
STOP RUN.
READ-MODIFY-PARA.
READ INFILE AT END MOVE -1 TO LAST.
IF LAST NOT = -1
IF NAME = SNAME
COMPUTE SFLAG = 1
DISPLAY "SEARCH FOUND".
DISPLAY INREC.
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
WORKING-STORAGE SECTION.
77 SFLAG PIC 99 VALUE 0.
77 SNAME PIC X(20).
77 LAST PIC S9 VALUE 0.
PROCEDURE DIVISION.
MAINPARA.
OPEN I-O INFILE.
DISPLAY "ENTER THE NAME TO SEARCH".
ACCEPT SNAME.
PERFORM READ-MODIFY-PARA UNTIL LAST = -1 OR SFLAG = 1.
IF SFLAG = 1
PERFORM ACCEPT-PARA.
IF SFLAG = 0
DISPLAY "NO RECORDS PRESENT".
CLOSE INFILE.
STOP RUN.
READ-MODIFY-PARA.
READ INFILE AT END MOVE -1 TO LAST.
IF LAST NOT = -1
IF NAME = SNAME
COMPUTE SFLAG = 1
DISPLAY "SEARCH FOUND"
DISPLAY INREC.
ACCEPT-PARA.
DISPLAY "ENTER MODIFIED VALU
ACCEPT REGNO.
ACCEPT NAME.
ACCEPT ADDRESS.
REWRITE INREC.
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
Slow - when the hit rate is low.
Complicated to change (insert, delete).
Fast - when the hit rate is high.
Most storage efficient.
Simple organization.
Dis-advantages
IDENTIFICATION DIVISION.
PROGRAM-ID. S2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STU-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD STU-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS “STU.DAT”
DATA RECORD IS STU-REC.
01 STU-REC.
02 SNO PIC 9(2).
02 SNAME PIC X(10).
WORKING-STORAGE SECTION.
01 ANS PIC X VALUE SPACE.
PROCEDURE DIVISION.
P-1.
DISPLAY(1 1) ERASE.
OPEN EXTEND STU-FILE.
PERFORM G-W-PARA UNTIL ANS = "N".
CLOSE STU-FILE.
STOP RUN.
G-W-PARA.
DISPLAY(1 1) ERASE.
DISPLAY(3 5) "SNO : ".
ACCEPT SNO.
DISPLAY(5 5) "SNAME : ".
ACCEPT SNAME.
WRITE STU-REC.
DISPLAY(10 5) "CONTINUE [ Y/ N ] : ".
ACCEPT ANS.
IDENTIFICATION DIVISION.
PROGRAM-ID. FILE1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OUTFILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT INFILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION
FD INFILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "IN.TXT".
01 INREC.
02 REGNO PIC 9(5).
02 NAME PIC X(20).
02 ADDRESS PIC X(20).
FD OUTFILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "FILEOUT.TXT".
01 OUTREC PIC X(75).
WORKING-STORAGE SECTION.
01 HEAD-1.
02 FILLER PIC X(25).
02 FILLER PIC X(12) VALUE "ABC PVT Ltd".
01 LINE-1.
02 FILLER PIC X(24).
02 FILLER PIC X(14) VALUE ALL "-".
01 LINE-2.
02 FILLER PIC X(75) VALUE ALL "-".
01 HEAD-2.
02 FILLER PIC X(8) VALUE "EMPNO".
02 FILLER PIC X(23) VALUE "EMP NAME".
02 FILLER PIC X(20) VALUE "ADDRESS".
01 DETAILS.
02 REG PIC 9(5).
02 FILLER PIC X(3).
02 NAM PIC X(20).
02 FILLER PIC X(3).
02 ADDR PIC X(40).
77 LAST PIC S9 VALUE 0.
PROCEDURE DIVISION.
MAINPARA.
OPEN INPUT INFILE.
OPEN OUTPUT OUTFILE.
MOVE HEAD-1 TO OUTREC.
WRITE OUTREC.
MOVE LINE-1 TO OUTREC.
WRITE OUTREC.
MOVE LINE-2 TO OUTREC.
WRITE OUTREC.
MOVE HEAD-2 TO OUTREC.
WRITE OUTREC.
MOVE LINE-2 TO OUTREC.
WRITE OUTREC.
PERFORM READ-WRITE-PARA UNTIL LAST = -1.
MOVE LINE-2 TO OUTREC.
WRITE OUTREC.
CLOSE INFILE, OUTFILE.
STOP RUN.
READ-WRITE-PARA.
READ INFILE AT END MOVE -1 TO LAST.
IF LAST NOT = -1
MOVE REGNO TO REG
MOVE NAME TO NAM
MOVE ADDRESS TO ADDR
MOVE DETAILS TO OUTREC
WRITE OUTREC.
IDENTIFICATION DIVISION.
PROGRAM-ID. S3.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STU-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
DATA DIVISION.
FILE SECTION.
FD STU-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS 'STU.DAT'
DATA RECORD IS STU-REC.
01 STU-REC.
02 SNO PIC 9(2).
02 SNAME PIC X(10).
WORKING-STORAGE SECTION.
01 N PIC 99.
PROCEDURE DIVISION.
P-1.
DISPLAY(1 1) ERASE.
DISPLAY “ENTER THE NUMBER”
ACCEPT N.
OPEN OUTPUT STU-FILE.
PERFORM G-W-PARA N TIMES.
CLOSE STU-FILE.
OPEN INPUT STU-FILE.
PERFORM OUTPARA N TIMES
CLOSE STU-FILE.
STOP RUN.
G-W-PARA.
DISPLAY(1 1) ERASE.
DISPLAY(3 5) "SNO : ".
ACCEPT SNO.
DISPLAY(5 5) "SNAME : ".
ACCEPT SNAME.
WRITE STU-REC.
OUTPARA.
READ STU-FILE.
DISPLAY “SNO=“ SNO “ NAME=“ SNAME.
• The process of sequencing the records in some desired
manner is known as sorting.
• Sorting is done upon some key data item in the record.
• Major key and minor key
SORT VERB
• SORT verb can be used to sort a sequential file.
• The MERGE verb can be used to merge several sorted
files to create a new file containing the records of these
files in the sorting order.
• The sort verb have different forms.
• The simple sort verb requires the three files
• the unsorted input file
• the sorted output file
• the work file.
The format of thesimple SORT verb is as follows:
Filename-1 is the name of the workfile
Filename-2  input file name
Filename-3  output file name
Filename-2 and filename-3 should be specified
through FD entry
The work file is to be defined by a sort description
entry(SD entry).
The format of SD entry is as follows.
The following rules
(i) The input, output as well as the work file are open by
the sort statement before the sorting begins and are closed
by the sort statement itself after the sorting is over.
(ii) There can be any number of SORT statement in a
program.
(iii) The sorting can be done on any number of keys.
(iv) All the keys on which the sorting is done, must appear
with their description in the record description of file name1.
(v) The SELECT clauses for the work file file- name-1 is
SELECT file-name-1 ASSIGN TO hardware-name.
A file for which a record having 2 fields, namely: Account
Number and Name is already available. Sort the file based
on the ascending order of Account Number.
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT O1-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT S1-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT W-FILE ASSIGN TO DISK.
DATA DIVISION.
FILE SECTION.
FD O1-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "O1.DAT".
01 O1-REC.
02 O1-ACC-NO PIC 9(2).
02 O1-NAME PIC X(4).
FD S1-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "S1.DAT".
01 S1-REC.
02 S1-ACC-NO PIC 9(2).
02 S1-NAME PIC X(4).
SD W-FILE.
01 W-REC.
02 W-ACC-NO PIC 9(2).
02 W-NAME PIC X(4).
Size of 3 files
should be same
No need to
Specify all
Fields in
workfile
PROCEDURE DIVISION.
P-1.
SORT W-FILE ON ASCENDING KEY W-ACC-NO
USING O1-FILE GIVING S1-FILE.
STOP RUN.
SIMPLE MERGE VERB
the merging of files is frequently required in various
commercial application.
It is possible to merge two or more files with one MERGE
statement.
The format of the simple MERGE verb is as follows.
The input files to be merged through the MERGE statement
are specified in the USING phrase.
These files must be sequential files and must be sorted on
the merge keys.
The rules of the SORT statement in respect of the
ASCENDING/DECENDING KEY phrase are also
applicable in this case.
ALL THE FILES IN THE MERGE STATEMENT MUST
HAVE RECORDS OF SAME SIZE AND THE
POSITION OF THE MERGE KEY WITHIN THE
RECORD DESCRIPTION OF EACH OF THE FILES
MUST BE SAME
TWO FILES FOR WHICH A RECORD HAVING 2 FIELDS
NAMELY ACCOUNT NUMBER AND NAME ARE
ALREADY AVAILABLE. MERGE THESE TWO FILES AND
CREATE A NEW FILE BASED ON THE ASCENDING
ORDER OF ACCOUNT NUMBER.
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT O1-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT O2-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
.
SELECT M-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT W-FILE ASSIGN TO DISK.
DATA DIVISION.
FILE SECTION.
FD O1-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "O1.DAT".
01 O1-REC.
02 O1-ACC-NO PIC 9(2).
02 O1-NAME PIC X(4).
FD O2-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "O2.DAT".
01 O2-REC.
02 O2-ACC-NO PIC 9(2).
02 O2-NAME PIC X(4).
FD M-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "M.DAT".
01 M-REC.
02 M-ACC-NO PIC 9(2).
02 M-NAME PIC X(4).
SD W-FILE.
01 W-REC.
02 W-ACC-NO PIC 9(2).
02 W-NAME PIC X(4).
PROCEDURE DIVISION.
P-1.
MERGE W-FILE ON ASCENDING KEY W-ACC-NO
USING O1-FILE O2-FILE GIVING M-FILE.
STOP RUN.
DIRECT ACCESS file
• Files which are stored on a direct access storage medium such as a
magnetic disk, are often called direct access files.
• COBOL supports three different organization for disk files
• Sequential
• relative
• and index sequential.
• A relative file is a magnetic-disk file organized in such
a way that each record is identified by a relative
record number.
• The relative record number specifies the position of
the record from the beginning of the file.
• Thus the relative record number 1identifies the first
record, the relative record number 2 identifies the
second record and so on.
• A relative file can be access either sequentially or
randomly.
RELATIVES file
• When the file is accessed sequentially the records are
accessed in the increasing order of their relative record
numbers.
• When a file is accessed randomly, the programmer must
specify the relative record number.
•In relative organization, the reading as well as the writing
can be done randomly.
•Thus when a file is created by writing the record randomly,
some of the record position may remain empty.
•While these positions can be filled in subsequently, the
programmer should avoid specifying these empty positions
while reading such as relative file randomly.
•If a relative file is read in a sequential manner, such empty
position within it, if any, are ignored.
The handling of relative files requires some special codes in
the FILE-CONTROL paragraph as well as in the
PROCEDURE DIVISION.
FILE-CONTROL paragraph for relative files:
The general format for the SELECT clause for a relative file
is as follows.
ACCESS mode  Sequential
Access mode  Random
ACCESS MODE IS DYNAMIC  file is accessed
sequentially or/and randomly in the Procedure
Division
The phrase RELATIVE KEY must be specified when
access mode is RANDOM or DYNAMIC
Dataname-1  called relative key data item 
relative record number
Programmer must place an appropriate value in the
relative key data item  to access randomly
Dataname-1  unsigned integer  must not be
part of the record description for the said relative file
Whether the file should be used sequentially or randomly,
should specified through the word SEQUENTIAL or
RANDOM in the access mode clause
the clause ACCESS MODE IS DYNAMIC it indicates that
the file is accessed sequentially and / or randomly in
the PROCEDURE DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT REL-FILE ASSIGN TO DISK
ORGANIZATION IS RELATIVE
ACCESS MODE IS DYNAMIC
RELATIVE KEY IS RK-NO.
FILE SECTION
FD REL-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "IN.TXT".
01 REL-REC.
02 REGNO PIC 9(5).
02 NAME PIC X(20).
02 ADDRESS PIC X(20).
WORKING-STORAGE SECTION.
77 RK-NO PIC 999.
The statements OPEN,CLOSE, READ,WRITE and
REWRITE which are available for sequential files are
available for the relative files.
In addition, two other words, namely,
DELETE and START are also available.
PROCEDURE DIVISION for relative files
READ STATEMENTS:
•As usual, a READ statements reads a record of the file.
•The file must be open in either the input or I-O mode.
There are 3 different general format for the read statements
This format is the normal form of the READ statements.
•Applicable to sequential file access
•Format 2 is used when the access mode is either
random or dynamic.
•Record to be read is identified from the value in the relative data
item
MOVE 50 TO REL-KEY.
READ EMP-FILE RECORD
INVALID KEY PERFORM NOT-FOUND-PARA
•Format 3 can be used when the Access mode is dynamic and
the records are to be read sequentially.
•The next record is identified according to the following rules:
(i) When the READ NEXT statements is the first
statement to be executed after the open statement on
the file, the next record is the first record of the file.
(ii) the execution of the READ NEXT statement
follows the execution of the start statement  next
record is the record logically positioned by the
START statement
READ REL-FILE next record AT END MOVE -1 TO LAST.
IF LAST NOT = -1
DISPLAY REL-REC.
WRITE Statement
The WRITE statement for a relative file has the following
format.
The file must be open either in the OUTPUT or I-O mode.
•File access is RANDOM  the statement releases the
record to that relative record position  indicated by
the REL-KEY
•In sequential access records are released to the file in
the sequential record position
The imperative statement of the INVALID KEY phrase is
execution in the following case:
(i) when an attempt is made to write beyond the
externally-defined boundaries of the file.
(ii) When an attempt is made to write in the record position
which already contains a valid record
DISPLAY "ENTER THE DETAILS".
ACCEPT REGNO.
ACCEPT NAME.
ACCEPT ADDRESS.
COMPUTE RK-NO = REGNO.
DISPLAY "ACCEPT THE VALUE OF RELKEY".
ACCEPT RK-NO.
WRITE REL-REC.
REWRITE STATEMENT:
The REWRITE statement has the following format for a relative file,
REWRITE record-name [ FROM identifier ]
[ ; INVALID KEY imperative –statement ]
• The REWRITE statement is used to replace an exiting
record by the contents of the record specified in the record
name.
• The file must be opened in the I-O mode.
Access mode  SEQUENTIAL
•prior to REWRITE a READ statement on the file
should be successfully executed
•No need to specify INVALID KEY option here
The imperative statement after the INVALID KEY is
executed when an attempt is made to replace a record position
which is empty
Access mode  RANDOM or DYNAMIC
•the record to be replaced is identified by the contents
of relative key data
DELETE STATEMENT:
The format of the DELETE statement is as follows:
DELETE file-name RECORD
[ ; INVALID KEY imperative-statement ]
•The file must be opened in I-O mode
• ACCESS MODE IS SEQUENTIAL
•the execution of the DELETE statement must be preceded by the
execution of a READ statement on the file and the INVALID KEY
phrase should not be specified.
INVALID KEY  when an attempt is made to delete the
record of an empty record position
START STATEMENT:
The format of the START statement is as follows:
START file-name KEY IS = DATANAME1
>
NOT <
[ ; INVALID KEY imperative-statement ]
•The START statement enable the programmer to position the
relative file at some specified point
•ie subsequent sequential operation on this file can start from
this point.
Open in INPUT or
I-O mode
INDEXED SEQUENTIAL FILES
Relative files  based on relative key
To overcome this problem
ie to identify the records
based on value in the record
field  index sequential file
INDEXED SEQUENTIAL FILES
•In indexed sequential files (also referred to as indexed files),
the records are stored in the key sequence order (ascending
order).
•In addition, some index tables are also created and
maintained with the file.
•An indexed file on COBOL can be accessed either sequential
or randomly.
•While creating an indexed file the records can be written
only sequentially and in the ascending order of the key.
•When an indexed file is accessed randomly, the sequence in
which the records are accessed is controlled by the
programmer by specifying the value of a data item called
record key.
•Indexed files in a COBOL program can be handled through
suitable special codes in the FILE-CONTROL paragraph
and in the PROCEDURE DIVISION.
FILE-CONTROL paragraph for indexed files
The general format for the SELECT clause for an files is as
follows:
The ORGANIZATION clause indicates that the file is an
indexed file.
The RECORD KEY clause specifies the record key data
item on the basis of which the file is sequenced. (prime key)
The dataname-1 must be an alphanumeric field within the
record description for the file
Alternate key  must be defined in the record description
•Two or more records having the same value for
the prime key are not allowed
•Duplicates are allowed for alternative keys
•Records of an indexed file can be accessed by
specifying the primary key or alternative key
READ STATEMENT
When access mode is RANDOM or DYNAMIC  the
records are to be read in a random manner,
READ file-name RECORD [ INTO IDENTIFIER ]
[ ; KEY IS data-name ]
[ ; INVALID KEY imperative-statement ]
•The data name in the KEY IS phrase must be either the prime key
or the alternative key item.
•If the phrase is not specified, the prime key is assumed.
MOVE “1234” TO EMPNO.
READ EMP-FILE REOCRD
KEY IS EMP-NO
INVALID KEY PERFORM NOT-FOUND-PARA
ACCESS MODE IS SEQUENTIAL OR DYNAMIC 
SAME SYNTAX OF RELATIVE FILE
WRITE STATEMENT
•The records are written to be logical position
as determined from the value of the record
key.
•OUTPUT MODE  the records must be
released in the ascending order of the record
key values regardless of the ACCESS mode
•Records can be released in any order only
when the file is opened in I-O mode and
Access mode is RANDOM OR DYNAMIC
The INVALID KEY condition arises in the following
cases:
(i) Attempt to write a record beyond the externally
defined boundaries of the file.
(ii) file is opened in the OUTPUT mode and the value of
the record key is not greater than the value of the record
key for the previous record written.
(iii) When the value of the record key is equal to the record
key of a record key already present in the file.
REWRITE STATEMENT
•File must be opened in the I-O mode,
•If SEQENTIAL access mode the value of the record
key of the record being replaced must be equal to that of
the record last read from this file.
The INVALID KEY condition arises
(i) when the record key does not match that of an existing
record in the file.
(ii) For SEQUENTIAL access, when the value of the
record key is not identical to that of the last record read
from the file.
DELETE STATEMENT
•Open  I-O mode
•If the access is SEQUENTIAL, the INVALID KEY phrase
should not be specified
•Before executing DELETE there must be successful
READ statement.
•For RANDOM or DYNAMIC  record to be deleted is
determined by the value of record key
•INVALID KEY phrase should be specified
START
Position the file to the first logical record
Access mode  SEQUENTIAL or DYNAMIC
File opened  I-O or INPUT

Contenu connexe

Tendances (20)

File operations in c
File operations in cFile operations in c
File operations in c
 
Unit 8
Unit 8Unit 8
Unit 8
 
File management
File managementFile management
File management
 
Unix system programming
Unix system programmingUnix system programming
Unix system programming
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File management53(1)
File management53(1)File management53(1)
File management53(1)
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
Filelist
FilelistFilelist
Filelist
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
File organization and indexing
File organization and indexingFile organization and indexing
File organization and indexing
 
File Management
File ManagementFile Management
File Management
 
Linux 4 you
Linux 4 youLinux 4 you
Linux 4 you
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Unit 3
Unit  3Unit  3
Unit 3
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examples
 

Similaire à File handling 2016

SEQFILE1.PPT
SEQFILE1.PPTSEQFILE1.PPT
SEQFILE1.PPTloverkodi
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O YourHelper1
 
Degonto, File management system in fisheries science
Degonto, File management  system in fisheries scienceDegonto, File management  system in fisheries science
Degonto, File management system in fisheries scienceDegonto Islam
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfsudhakargeruganti
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYRajeshkumar Reddy
 
File system interface
File system interfaceFile system interface
File system interfaceDayan Ahmed
 
FILE SYSTEMS IN WINDOWS OPERATING SYSTEMS
FILE SYSTEMS IN WINDOWS OPERATING SYSTEMSFILE SYSTEMS IN WINDOWS OPERATING SYSTEMS
FILE SYSTEMS IN WINDOWS OPERATING SYSTEMSKABILESH RAMAR
 
SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012Rian Yulian
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systemsvampugani
 
MODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptxMODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptxManasaPJ1
 

Similaire à File handling 2016 (20)

SEQFILE1.PPT
SEQFILE1.PPTSEQFILE1.PPT
SEQFILE1.PPT
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
Degonto, File management system in fisheries science
Degonto, File management  system in fisheries scienceDegonto, File management  system in fisheries science
Degonto, File management system in fisheries science
 
Introduction to c part 4
Introduction to c  part  4Introduction to c  part  4
Introduction to c part 4
 
Robocopy
RobocopyRobocopy
Robocopy
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
File system interface
File system interfaceFile system interface
File system interface
 
FILE SYSTEMS IN WINDOWS OPERATING SYSTEMS
FILE SYSTEMS IN WINDOWS OPERATING SYSTEMSFILE SYSTEMS IN WINDOWS OPERATING SYSTEMS
FILE SYSTEMS IN WINDOWS OPERATING SYSTEMS
 
File handling
File handlingFile handling
File handling
 
SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File management
File managementFile management
File management
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systems
 
File mangement
File mangementFile mangement
File mangement
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
File Management System in Shell Script.pptx
File Management System in Shell Script.pptxFile Management System in Shell Script.pptx
File Management System in Shell Script.pptx
 
MODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptxMODULE 3.1 updated-18cs56.pptx
MODULE 3.1 updated-18cs56.pptx
 

Dernier

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Dernier (20)

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

File handling 2016

  • 1.
  • 2. Objectives of the Session (1) Introduction to File handling. (2) File ORGANIZATION and ACCESS methods. (3) File handling verbs. (4) Sequential File processing. (5) Demonstration of sample programs.
  • 3. FILES  COBOL is generally used in situations where the volume of data to be processed is large.  TO STORE LARGE VOLUMES OF DATA AND RETRIEVE IT LATER IN PROGRAMMING  To store Values(data) permanently
  • 4. Sequential files  Records can be accessed in the order of their appearance  Magnetic tape file  sequential  Disk file  sequential, random etc
  • 5. Introduction to File processing Field - Field type and Field size. FIELD to describe an item of information we are recording about an object (e.g. StudentName, DateOfBirth, CourseCode).  Record Record-Size, Fixed length records and Variable length records. 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).  Basic Terminologies 01 StudentRecord. 02 regno pic 9999. 02 name pic x(20). 02 address pic x(20)
  • 6. Files. File Master files, Transaction files, File organization and File access method. FILE to describe a collection of one or more occurrences (instances) of a record type (template).
  • 7.  Record occurrence (i.e. the values of a record)  Record type (i.e. the structure of the record). Every record in a file has a different value but the same structure.
  • 8. Files, Records, Fields. StudId StudName DateOfBirth 9723456 COUGHLAN 10091961 9724567 RYAN 31121976 9534118 COFFEY 23061964 9423458 O'BRIEN 03111979 9312876 SMITH 12121976 STUDENTS DATA DIVISION. FILE SECTION. FD StudentFile. 01 StudentDetails. 02 StudId PIC 9(7). 02 StudName PIC X(8). 02 DateOfBirth PIC X(8). occurrences Record Type (Template) (Structure)
  • 9. 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
  • 10. FILE CHARACTERISTICS The task of file handling is the responsibility of the system software known as IOCS (Input- Output control system). Programmer  only specify file characteristics
  • 11. Record Size: The size of the records may be chosen by the programmer. Records contain fixed and/or variables size records DATA DIVISION. FILE SECTION. FD StudentFile. 01 StudentDetails. 02 StudId PIC 9(7). 02 StudName PIC X(8). 02 DateOfBirth PIC X(8).
  • 12. Block Size: •group a number of consecutive records to form a block •The number of records in a block is often called “blocking factors”. •The actual handling of blocks is transparent to the programmer  specify no. of records in a block •Advantage  saving of input output time Memory can be saved (inter record gap) •This memory space is known as the buffer.
  • 13. Buffers:  In Modern computers  I-O operations independent of the CPU by means of the hardware  data channel.  Enable overlapping of I-O with other CPU operation  To take advantage of this situation --> more than one buffer for a file
  • 14. Organization and Access Two important characteristics of files are – DATA ORGANIZATION – METHOD OF ACCESS
  • 15. 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.
  • 16. METHOD OF ACCESS  The method of access refers to the way in which records are accessed. (sequential and random) – 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.
  • 17. 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.
  • 18. FILE-CONTROL paragraph for sequential files SELECT file-name ASSIGN TO hardware-name [ RESERVE integer-1 { AREA/AREAS} [ ORGANIZATION IS SEQUENTIAL ] [ ACCESS MODE IS SEQUENTIAL] [ FILE STATUS IS identifier ]. ORGANIZATION/ACCESS : •Optional •Default sequential organization and sequential access
  • 19. RESERVE: • The number of buffers used for the file. •RESERVE 1 AREA means only one buffer is to be used •Optional  default two buffers FILE STATUS: VALUES-alphanumeric values. 00:-Successful completion 10:- At end condition 30:- Permanent error condition 34:-Boundary violation
  • 20. STUDENTS The Select and Assign Clause. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT StudentFile ASSIGN TO DISK organization is sequential. DISK
  • 21. FD entries for Fixed length records FD file-name [ RECORD CONTAINS integer-1 CHARACTERS ] [ BLOCK CONTAINS integer-2 { RECORDS, CHARACTERS }] [ LABEL { RECORD IS, RECORDS ARE } { STANDARD, OMITTED }] [VALUE OF implementor-name-1 IS {dataname-2, literal-2] [ DATA { RECORD IS, RECORDS ARE } identifier-1, identifier-2, . . . ].
  • 22. BLOCK CONTAINS CLAUSE: •Integer –1 of the BLOCK CONTAINS clause specifies the block size either in terms of records or in terms of characters. •For example, BLOCK CONTAINS 50 RECORDS means that there are 50 records in the block. •If omitted -- > one record per block RECORDS CONTAINS CLAUSE: •This clause specifies the records size. •Integer-1 specifies the number of characters in a record. •The RECORD CONTAINS clause is used for documentary purposes only.
  • 23. LABEL RECORD CLAUSE: This clause specifies whether or not the standard header and trailer labels should be present in the magnetic-tape files. Label Records clause for disk files should be specified with Standard VALUE OF CLAUSE: The VALUE OF clause is entirely implementation- dependent. In most compilers this clause is used to specify a file title. The clause in such cases has the form.
  • 24. DATA RECORD CLAUSE: •This clause documents the record names defined for the file. •For example, DATA RECORDS ARE REC-1, REC- 2, REC-3 means that there are three different record descriptions following the FD entry in which this DATA RECORDS clause is used.
  • 25. Example of file-description entries: FD FILE – A RECORD CONTAINS 130 CHARACTERS BLOCK CONTAINS 20 RECORDS DATA RECORD IS FIRST-RECORD LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS “MY-LIFE.txt”. 01 FIRST-RECORD PIC X(130).
  • 26. Record Buffers IDENTIFICATION DIVISION. etc. ENVIRONMENT DIVISION. etc. DATA DIVISION. FILE SECTION. Program RecordBuffer Declaration STUDENTS DISK Record Instance 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
  • 27.  To create an OUTPUT file containing data records each record must be placed in the record buffer and then transferred (written) to the file.
  • 28. IDENTIFICATION DIVISION. PROGRAM-ID. FIRSTFILE1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OUTFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION FD OUTFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "OUT.TXT". 01 OUTREC. 02 REG PIC 9(5). 02 NAM PIC X(20). 02 ADDR PIC X(40). WORKING-STORAGE SECTION. 77 A PIC 999.
  • 29. PROCEDURE DIVISION. MAINPARA. OPEN OUTPUT OUTFILE. ACCEPT REG. ACCEPT NAM. ACCEPT ADDR. WRITE OUTREC. CLOSE OUTFILE. OPEN INPUT OUTFILE. READ OUTFILE. CLOSE OUTFILE. STOP RUN. FD OUTFILE 01 OUTREC. 02 REG PIC 9(5). 02 NAM PIC X(20). 02 ADDR PIC X(40).
  • 30. MAINPARA. DISPLAY "ENTER NUMBER OF RECORDS". ACCEPT N. OPEN OUTPUT OUTFILE. PERFORM ACCEPT-PARA N TIMES. CLOSE OUTFILE. OPEN INPUT OUTFILE. DISPLAY " THE RECORD DISPLAYED AFTER READING". PERFORM DISPLAY-PARA N TIMES. CLOSE OUTFILE. STOP RUN. ACCEPT-PARA. DISPLAY "ENTER THE VALUE FOR EMP RECORD". ACCEPT REG. ACCEPT NAM. ACCEPT ADDR. WRITE OUTREC. DISPLAY-PARA. READ OUTFILE. DISPLAY OUTREC.
  • 31. File handling verbs  OPEN  READ  WRITE  REWRITE  CLOSE
  • 32. COBOL file handling Verbs  OPEN Before your program can access the data in an input file or place data in an output file  program OPEN 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.
  • 33. OPEN verb  Syntax OPEN {INPUT, OUTPUT, I-O, EXTEND} Filename-1 . . . OPEN MODE STATEMENT INPUT OUTPUT I-O EXTEND READ WRITE REWRITE
  • 34. The READ verb  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 purpose.
  • 35. READ verb syntax  The InternalFilename specified must be a file that has been OPENed for INPUT.  The NEXT RECORD clause is optional and generally not used.     lockStatementBENDAT IdentifierINTO RECORDNEXTlenameInternalFiREAD
  • 36.  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.
  • 37. Working of the READ statement STUD-REC 1 0 1 J Y O T H I 2 5B U REGNO NAME AGE 1 3 A H N 2B EOF 1 2 I H A 2B 0 R C A 0U 0 N T Y 2U A STUDENT PERFORM P1 UNTIL LAST = -1 . . . . P1. READ STUDFILE AT END MOVE -1 TO LAST
  • 38. Working of the READ statement STUD-REC 1 0 1 J Y O T H I 2 5B U REGNO NAME AGE 1 3 A H N 2B EOF 1 2 I H A 2B 0 R C A 0U 0 N T Y 2U A STUDENT 1 0 1 J Y O T H I 2 5B U PERFORM P1 UNTIL LAST = -1 . . . . P1. READ STUDFILE AT END MOVE -1 TO LAST
  • 39. Working of the READ statement 1 0 1 J Y O T H I 2 5B U REGNO NAME AGE 1 3 A H N 2B EOF 0 R C A 0U A 1 0 2 N I T H Y A 2 2B U 1 0 2 N I T H Y A 2 2B U PERFORM P1 UNTIL LAST = -1 . . . . P1. READ STUDFILE AT END MOVE -1 TO LAST STUD-REC STUDENT
  • 40. Working of the READ statement 1 0 1 J Y O T H I 2 5B U REGNO NAME AGE EOF 1 0 3 R A C H A N 2 0B U 1 0 2 N I T H Y A 2 2B U A 1 0 3 R A C H A N 2 0B U A PERFORM P1 UNTIL LAST = -1 . . . . P1. READ STUDFILE AT END MOVE -1 TO LAST STUD-REC STUDENT
  • 41. Working of the READ statement 1 0 1 J Y O T H I 2 5B U REGNO NAME AGE EOF J J J J J J J J J J JJ J 1 0 2 N I T H Y A 2 2B U J 1 0 3 R A C H A N 2 0B U A J J J J JJ JJ PERFORM P1 UNTIL LAST = -1 . . . . P1. READ STUDFILE AT END MOVE -1 TO LAST STUD-REC STUDENT
  • 42. WRITE Syntax.  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.  WRITE ADVANCING AdvanceNum MnemonicName PAGE RecordName FROM Identifier BEFORE AFTER LINE LINES                                          
  • 43. F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1 StudentID StudentName Course. StudentRecord F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1 EOF How the WRITE works OPEN OUTPUT StudentFile. MOVE "9334567Frank Curtain LM051" TO StudentDetails. WRITE StudentDetails. MOVE "9383715Thomas Healy LM068" TO StudentDetails. WRITE StudentDetails. CLOSE StudentFile. STOP RUN. Students
  • 44. Working of the WRITE statement STUD-REC 1 0 1 J Y O T H I 2 5B U REGNO NAME AGE EOF 1 0 1 J Y O T H I 2 5U MOVE "BU101" TO REGNO MOVE "JYOTHI" TO NAME MOVE 25 TO AGE. WRITE STUD-REC. . B STUDENT 01 STUD-REC 02 REGNO PIC X(5). 02 NAME PIC X(15). 02 AGE PIC 99.
  • 45. Working of the WRITE statement REGNO NAME AGE EOF 1 0 2 N I T H Y A 2 2B U 1 0 2 N I T H Y A 2 2B U 1 0 1 J Y O T H I 2 5UB STUD-REC STUDENT MOVE “BU101JYOTHI 25” TO STUD-REC. WRITE STUD-REC. MOVE “BU102NITHYA 22” TO STUD-REC. WRITE STUD-REC.
  • 46.  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
  • 47. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OUTFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. SELECT INFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION FD INFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "IN.TXT". 01 INREC. 02 REGNO PIC 9(5). 02 NAME PIC X(20). 02 ADDRESS PIC X(40). FD OUTFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "FILEOUT.TXT". 01 OUTREC. 02 REG PIC 9(5). 02 FILLER PIC X(3). 02 NAM PIC X(20). 02 FILLER PIC X(3). 02 ADDR PIC X(40). WORKING-STORAGE SECTION. 77 LAST PIC S9 VALUE 0.
  • 48. PROCEDURE DIVISION. MAINPARA. OPEN INPUT INFILE. OPEN OUTPUT OUTFILE. PERFORM READ-WRITE-PARA UNTIL LAST = -1. CLOSE INFILE, OUTFILE. STOP RUN. READ-WRITE-PARA. READ INFILE AT END MOVE -1 TO LAST. IF LAST NOT = -1 MOVE REGNO TO REG MOVE NAME TO NAM MOVE ADDRESS TO ADDR DISPLAY OUTREC WRITE OUTREC.
  • 49. To search a record in a file - open the file - accept the value of record to find - perform the readpara until last = -1 - - in readpara - read each record at end move -1 to last - after reading check whether that record is the last record
  • 50. WORKING-STORAGE SECTION. 77 SFLAG PIC 99 VALUE 0. 77 SNAME PIC X(20). 77 LAST PIC S9 VALUE 0. PROCEDURE DIVISION. MAINPARA. OPEN INPUT INFILE. DISPLAY "ENTER THE NAME TO SEARCH". ACCEPT SNAME. PERFORM READ-MODIFY-PARA UNTIL LAST = -1. IF SFLAG = 0 DISPLAY "NO RECORDS PRESENT". CLOSE INFILE. STOP RUN. READ-MODIFY-PARA. READ INFILE AT END MOVE -1 TO LAST. IF LAST NOT = -1 IF NAME = SNAME COMPUTE SFLAG = 1 DISPLAY "SEARCH FOUND". DISPLAY INREC.
  • 51. 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
  • 52. WORKING-STORAGE SECTION. 77 SFLAG PIC 99 VALUE 0. 77 SNAME PIC X(20). 77 LAST PIC S9 VALUE 0. PROCEDURE DIVISION. MAINPARA. OPEN I-O INFILE. DISPLAY "ENTER THE NAME TO SEARCH". ACCEPT SNAME. PERFORM READ-MODIFY-PARA UNTIL LAST = -1 OR SFLAG = 1. IF SFLAG = 1 PERFORM ACCEPT-PARA. IF SFLAG = 0 DISPLAY "NO RECORDS PRESENT". CLOSE INFILE. STOP RUN. READ-MODIFY-PARA. READ INFILE AT END MOVE -1 TO LAST. IF LAST NOT = -1 IF NAME = SNAME COMPUTE SFLAG = 1 DISPLAY "SEARCH FOUND" DISPLAY INREC. ACCEPT-PARA. DISPLAY "ENTER MODIFIED VALU ACCEPT REGNO. ACCEPT NAME. ACCEPT ADDRESS. REWRITE INREC.
  • 53. 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.
  • 54. Sequential files - A Final Look Advantages Slow - when the hit rate is low. Complicated to change (insert, delete). Fast - when the hit rate is high. Most storage efficient. Simple organization. Dis-advantages
  • 55. IDENTIFICATION DIVISION. PROGRAM-ID. S2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STU-FILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL ACCESS MODE IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD STU-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS “STU.DAT” DATA RECORD IS STU-REC. 01 STU-REC. 02 SNO PIC 9(2). 02 SNAME PIC X(10). WORKING-STORAGE SECTION. 01 ANS PIC X VALUE SPACE.
  • 56. PROCEDURE DIVISION. P-1. DISPLAY(1 1) ERASE. OPEN EXTEND STU-FILE. PERFORM G-W-PARA UNTIL ANS = "N". CLOSE STU-FILE. STOP RUN. G-W-PARA. DISPLAY(1 1) ERASE. DISPLAY(3 5) "SNO : ". ACCEPT SNO. DISPLAY(5 5) "SNAME : ". ACCEPT SNAME. WRITE STU-REC. DISPLAY(10 5) "CONTINUE [ Y/ N ] : ". ACCEPT ANS.
  • 57. IDENTIFICATION DIVISION. PROGRAM-ID. FILE1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OUTFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. SELECT INFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION FD INFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "IN.TXT". 01 INREC. 02 REGNO PIC 9(5). 02 NAME PIC X(20). 02 ADDRESS PIC X(20).
  • 58. FD OUTFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "FILEOUT.TXT". 01 OUTREC PIC X(75).
  • 59. WORKING-STORAGE SECTION. 01 HEAD-1. 02 FILLER PIC X(25). 02 FILLER PIC X(12) VALUE "ABC PVT Ltd". 01 LINE-1. 02 FILLER PIC X(24). 02 FILLER PIC X(14) VALUE ALL "-". 01 LINE-2. 02 FILLER PIC X(75) VALUE ALL "-". 01 HEAD-2. 02 FILLER PIC X(8) VALUE "EMPNO". 02 FILLER PIC X(23) VALUE "EMP NAME". 02 FILLER PIC X(20) VALUE "ADDRESS". 01 DETAILS. 02 REG PIC 9(5). 02 FILLER PIC X(3). 02 NAM PIC X(20). 02 FILLER PIC X(3). 02 ADDR PIC X(40). 77 LAST PIC S9 VALUE 0.
  • 60. PROCEDURE DIVISION. MAINPARA. OPEN INPUT INFILE. OPEN OUTPUT OUTFILE. MOVE HEAD-1 TO OUTREC. WRITE OUTREC. MOVE LINE-1 TO OUTREC. WRITE OUTREC. MOVE LINE-2 TO OUTREC. WRITE OUTREC. MOVE HEAD-2 TO OUTREC. WRITE OUTREC. MOVE LINE-2 TO OUTREC. WRITE OUTREC.
  • 61. PERFORM READ-WRITE-PARA UNTIL LAST = -1. MOVE LINE-2 TO OUTREC. WRITE OUTREC. CLOSE INFILE, OUTFILE. STOP RUN. READ-WRITE-PARA. READ INFILE AT END MOVE -1 TO LAST. IF LAST NOT = -1 MOVE REGNO TO REG MOVE NAME TO NAM MOVE ADDRESS TO ADDR MOVE DETAILS TO OUTREC WRITE OUTREC.
  • 62. IDENTIFICATION DIVISION. PROGRAM-ID. S3. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STU-FILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL ACCESS MODE IS SEQUENTIAL DATA DIVISION. FILE SECTION. FD STU-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS 'STU.DAT' DATA RECORD IS STU-REC. 01 STU-REC. 02 SNO PIC 9(2). 02 SNAME PIC X(10). WORKING-STORAGE SECTION. 01 N PIC 99.
  • 63. PROCEDURE DIVISION. P-1. DISPLAY(1 1) ERASE. DISPLAY “ENTER THE NUMBER” ACCEPT N. OPEN OUTPUT STU-FILE. PERFORM G-W-PARA N TIMES. CLOSE STU-FILE. OPEN INPUT STU-FILE. PERFORM OUTPARA N TIMES CLOSE STU-FILE. STOP RUN. G-W-PARA. DISPLAY(1 1) ERASE. DISPLAY(3 5) "SNO : ". ACCEPT SNO. DISPLAY(5 5) "SNAME : ". ACCEPT SNAME. WRITE STU-REC. OUTPARA. READ STU-FILE. DISPLAY “SNO=“ SNO “ NAME=“ SNAME.
  • 64. • The process of sequencing the records in some desired manner is known as sorting. • Sorting is done upon some key data item in the record. • Major key and minor key SORT VERB
  • 65. • SORT verb can be used to sort a sequential file. • The MERGE verb can be used to merge several sorted files to create a new file containing the records of these files in the sorting order. • The sort verb have different forms. • The simple sort verb requires the three files • the unsorted input file • the sorted output file • the work file.
  • 66. The format of thesimple SORT verb is as follows: Filename-1 is the name of the workfile Filename-2  input file name Filename-3  output file name Filename-2 and filename-3 should be specified through FD entry
  • 67. The work file is to be defined by a sort description entry(SD entry). The format of SD entry is as follows.
  • 68. The following rules (i) The input, output as well as the work file are open by the sort statement before the sorting begins and are closed by the sort statement itself after the sorting is over. (ii) There can be any number of SORT statement in a program. (iii) The sorting can be done on any number of keys.
  • 69. (iv) All the keys on which the sorting is done, must appear with their description in the record description of file name1. (v) The SELECT clauses for the work file file- name-1 is SELECT file-name-1 ASSIGN TO hardware-name.
  • 70. A file for which a record having 2 fields, namely: Account Number and Name is already available. Sort the file based on the ascending order of Account Number. IDENTIFICATION DIVISION. PROGRAM-ID. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT O1-FILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. SELECT S1-FILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. SELECT W-FILE ASSIGN TO DISK.
  • 71. DATA DIVISION. FILE SECTION. FD O1-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "O1.DAT". 01 O1-REC. 02 O1-ACC-NO PIC 9(2). 02 O1-NAME PIC X(4). FD S1-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "S1.DAT". 01 S1-REC. 02 S1-ACC-NO PIC 9(2). 02 S1-NAME PIC X(4). SD W-FILE. 01 W-REC. 02 W-ACC-NO PIC 9(2). 02 W-NAME PIC X(4). Size of 3 files should be same No need to Specify all Fields in workfile
  • 72. PROCEDURE DIVISION. P-1. SORT W-FILE ON ASCENDING KEY W-ACC-NO USING O1-FILE GIVING S1-FILE. STOP RUN.
  • 73. SIMPLE MERGE VERB the merging of files is frequently required in various commercial application. It is possible to merge two or more files with one MERGE statement. The format of the simple MERGE verb is as follows.
  • 74. The input files to be merged through the MERGE statement are specified in the USING phrase. These files must be sequential files and must be sorted on the merge keys. The rules of the SORT statement in respect of the ASCENDING/DECENDING KEY phrase are also applicable in this case. ALL THE FILES IN THE MERGE STATEMENT MUST HAVE RECORDS OF SAME SIZE AND THE POSITION OF THE MERGE KEY WITHIN THE RECORD DESCRIPTION OF EACH OF THE FILES MUST BE SAME
  • 75. TWO FILES FOR WHICH A RECORD HAVING 2 FIELDS NAMELY ACCOUNT NUMBER AND NAME ARE ALREADY AVAILABLE. MERGE THESE TWO FILES AND CREATE A NEW FILE BASED ON THE ASCENDING ORDER OF ACCOUNT NUMBER. IDENTIFICATION DIVISION. PROGRAM-ID. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION.
  • 76. FILE-CONTROL. SELECT O1-FILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. SELECT O2-FILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. . SELECT M-FILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. SELECT W-FILE ASSIGN TO DISK.
  • 77. DATA DIVISION. FILE SECTION. FD O1-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "O1.DAT". 01 O1-REC. 02 O1-ACC-NO PIC 9(2). 02 O1-NAME PIC X(4). FD O2-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "O2.DAT". 01 O2-REC. 02 O2-ACC-NO PIC 9(2). 02 O2-NAME PIC X(4).
  • 78. FD M-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "M.DAT". 01 M-REC. 02 M-ACC-NO PIC 9(2). 02 M-NAME PIC X(4). SD W-FILE. 01 W-REC. 02 W-ACC-NO PIC 9(2). 02 W-NAME PIC X(4).
  • 79. PROCEDURE DIVISION. P-1. MERGE W-FILE ON ASCENDING KEY W-ACC-NO USING O1-FILE O2-FILE GIVING M-FILE. STOP RUN.
  • 80. DIRECT ACCESS file • Files which are stored on a direct access storage medium such as a magnetic disk, are often called direct access files. • COBOL supports three different organization for disk files • Sequential • relative • and index sequential.
  • 81. • A relative file is a magnetic-disk file organized in such a way that each record is identified by a relative record number. • The relative record number specifies the position of the record from the beginning of the file. • Thus the relative record number 1identifies the first record, the relative record number 2 identifies the second record and so on. • A relative file can be access either sequentially or randomly. RELATIVES file
  • 82. • When the file is accessed sequentially the records are accessed in the increasing order of their relative record numbers. • When a file is accessed randomly, the programmer must specify the relative record number.
  • 83. •In relative organization, the reading as well as the writing can be done randomly. •Thus when a file is created by writing the record randomly, some of the record position may remain empty. •While these positions can be filled in subsequently, the programmer should avoid specifying these empty positions while reading such as relative file randomly. •If a relative file is read in a sequential manner, such empty position within it, if any, are ignored.
  • 84. The handling of relative files requires some special codes in the FILE-CONTROL paragraph as well as in the PROCEDURE DIVISION. FILE-CONTROL paragraph for relative files: The general format for the SELECT clause for a relative file is as follows.
  • 85. ACCESS mode  Sequential Access mode  Random ACCESS MODE IS DYNAMIC  file is accessed sequentially or/and randomly in the Procedure Division The phrase RELATIVE KEY must be specified when access mode is RANDOM or DYNAMIC Dataname-1  called relative key data item  relative record number Programmer must place an appropriate value in the relative key data item  to access randomly Dataname-1  unsigned integer  must not be part of the record description for the said relative file
  • 86. Whether the file should be used sequentially or randomly, should specified through the word SEQUENTIAL or RANDOM in the access mode clause the clause ACCESS MODE IS DYNAMIC it indicates that the file is accessed sequentially and / or randomly in the PROCEDURE DIVISION.
  • 87. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REL-FILE ASSIGN TO DISK ORGANIZATION IS RELATIVE ACCESS MODE IS DYNAMIC RELATIVE KEY IS RK-NO. FILE SECTION FD REL-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "IN.TXT". 01 REL-REC. 02 REGNO PIC 9(5). 02 NAME PIC X(20). 02 ADDRESS PIC X(20). WORKING-STORAGE SECTION. 77 RK-NO PIC 999.
  • 88. The statements OPEN,CLOSE, READ,WRITE and REWRITE which are available for sequential files are available for the relative files. In addition, two other words, namely, DELETE and START are also available. PROCEDURE DIVISION for relative files
  • 89. READ STATEMENTS: •As usual, a READ statements reads a record of the file. •The file must be open in either the input or I-O mode. There are 3 different general format for the read statements
  • 90. This format is the normal form of the READ statements. •Applicable to sequential file access
  • 91. •Format 2 is used when the access mode is either random or dynamic. •Record to be read is identified from the value in the relative data item MOVE 50 TO REL-KEY. READ EMP-FILE RECORD INVALID KEY PERFORM NOT-FOUND-PARA
  • 92. •Format 3 can be used when the Access mode is dynamic and the records are to be read sequentially. •The next record is identified according to the following rules: (i) When the READ NEXT statements is the first statement to be executed after the open statement on the file, the next record is the first record of the file. (ii) the execution of the READ NEXT statement follows the execution of the start statement  next record is the record logically positioned by the START statement
  • 93. READ REL-FILE next record AT END MOVE -1 TO LAST. IF LAST NOT = -1 DISPLAY REL-REC.
  • 94. WRITE Statement The WRITE statement for a relative file has the following format. The file must be open either in the OUTPUT or I-O mode. •File access is RANDOM  the statement releases the record to that relative record position  indicated by the REL-KEY •In sequential access records are released to the file in the sequential record position
  • 95. The imperative statement of the INVALID KEY phrase is execution in the following case: (i) when an attempt is made to write beyond the externally-defined boundaries of the file. (ii) When an attempt is made to write in the record position which already contains a valid record
  • 96. DISPLAY "ENTER THE DETAILS". ACCEPT REGNO. ACCEPT NAME. ACCEPT ADDRESS. COMPUTE RK-NO = REGNO. DISPLAY "ACCEPT THE VALUE OF RELKEY". ACCEPT RK-NO. WRITE REL-REC.
  • 97. REWRITE STATEMENT: The REWRITE statement has the following format for a relative file, REWRITE record-name [ FROM identifier ] [ ; INVALID KEY imperative –statement ] • The REWRITE statement is used to replace an exiting record by the contents of the record specified in the record name. • The file must be opened in the I-O mode.
  • 98. Access mode  SEQUENTIAL •prior to REWRITE a READ statement on the file should be successfully executed •No need to specify INVALID KEY option here The imperative statement after the INVALID KEY is executed when an attempt is made to replace a record position which is empty Access mode  RANDOM or DYNAMIC •the record to be replaced is identified by the contents of relative key data
  • 99. DELETE STATEMENT: The format of the DELETE statement is as follows: DELETE file-name RECORD [ ; INVALID KEY imperative-statement ] •The file must be opened in I-O mode • ACCESS MODE IS SEQUENTIAL •the execution of the DELETE statement must be preceded by the execution of a READ statement on the file and the INVALID KEY phrase should not be specified. INVALID KEY  when an attempt is made to delete the record of an empty record position
  • 100. START STATEMENT: The format of the START statement is as follows: START file-name KEY IS = DATANAME1 > NOT < [ ; INVALID KEY imperative-statement ] •The START statement enable the programmer to position the relative file at some specified point •ie subsequent sequential operation on this file can start from this point. Open in INPUT or I-O mode
  • 101. INDEXED SEQUENTIAL FILES Relative files  based on relative key To overcome this problem ie to identify the records based on value in the record field  index sequential file
  • 102. INDEXED SEQUENTIAL FILES •In indexed sequential files (also referred to as indexed files), the records are stored in the key sequence order (ascending order). •In addition, some index tables are also created and maintained with the file. •An indexed file on COBOL can be accessed either sequential or randomly. •While creating an indexed file the records can be written only sequentially and in the ascending order of the key.
  • 103. •When an indexed file is accessed randomly, the sequence in which the records are accessed is controlled by the programmer by specifying the value of a data item called record key. •Indexed files in a COBOL program can be handled through suitable special codes in the FILE-CONTROL paragraph and in the PROCEDURE DIVISION.
  • 104. FILE-CONTROL paragraph for indexed files The general format for the SELECT clause for an files is as follows:
  • 105. The ORGANIZATION clause indicates that the file is an indexed file. The RECORD KEY clause specifies the record key data item on the basis of which the file is sequenced. (prime key) The dataname-1 must be an alphanumeric field within the record description for the file Alternate key  must be defined in the record description
  • 106. •Two or more records having the same value for the prime key are not allowed •Duplicates are allowed for alternative keys •Records of an indexed file can be accessed by specifying the primary key or alternative key
  • 107. READ STATEMENT When access mode is RANDOM or DYNAMIC  the records are to be read in a random manner, READ file-name RECORD [ INTO IDENTIFIER ] [ ; KEY IS data-name ] [ ; INVALID KEY imperative-statement ] •The data name in the KEY IS phrase must be either the prime key or the alternative key item. •If the phrase is not specified, the prime key is assumed. MOVE “1234” TO EMPNO. READ EMP-FILE REOCRD KEY IS EMP-NO INVALID KEY PERFORM NOT-FOUND-PARA
  • 108. ACCESS MODE IS SEQUENTIAL OR DYNAMIC  SAME SYNTAX OF RELATIVE FILE
  • 109. WRITE STATEMENT •The records are written to be logical position as determined from the value of the record key. •OUTPUT MODE  the records must be released in the ascending order of the record key values regardless of the ACCESS mode •Records can be released in any order only when the file is opened in I-O mode and Access mode is RANDOM OR DYNAMIC
  • 110. The INVALID KEY condition arises in the following cases: (i) Attempt to write a record beyond the externally defined boundaries of the file. (ii) file is opened in the OUTPUT mode and the value of the record key is not greater than the value of the record key for the previous record written. (iii) When the value of the record key is equal to the record key of a record key already present in the file.
  • 111. REWRITE STATEMENT •File must be opened in the I-O mode, •If SEQENTIAL access mode the value of the record key of the record being replaced must be equal to that of the record last read from this file. The INVALID KEY condition arises (i) when the record key does not match that of an existing record in the file. (ii) For SEQUENTIAL access, when the value of the record key is not identical to that of the last record read from the file.
  • 112. DELETE STATEMENT •Open  I-O mode •If the access is SEQUENTIAL, the INVALID KEY phrase should not be specified •Before executing DELETE there must be successful READ statement. •For RANDOM or DYNAMIC  record to be deleted is determined by the value of record key •INVALID KEY phrase should be specified
  • 113. START Position the file to the first logical record Access mode  SEQUENTIAL or DYNAMIC File opened  I-O or INPUT