Cobol File Handling

37
File Handling in COBOL

description

cobol file handling concepts

Transcript of Cobol File Handling

Page 1: Cobol File Handling

File Handling in COBOL

Page 2: Cobol File Handling

Topics covered in the SessionTopics covered in the Session

(1) Introduction to File handling.

(2) File ORGANIZATION and ACCESS methods.

(3) File handling verbs.

Page 3: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

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

These systems are sometimes These systems are sometimes referred to as “data intensive” referred to as “data intensive” systems.systems.

Page 4: Cobol File Handling

Introduction to File Introduction to File processingprocessing

FieldField Field type and Field size.Field type and Field size.

RecordRecord Record-Size, Fixed length records and Record-Size, Fixed length records and Variable length records.Variable length records.

FileFile Master files, Transaction files, File Master files, Transaction files, File organization and File access method.organization and File access method.

Basic Terminologies

Page 5: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

Files, Records, Files, Records, Fields.Fields.

We use the term FIELD to describe an item of We use the term FIELD to describe an item of information we are recording about an objectinformation we are recording about an object

(e.g. StudentName, DateOfBirth, CourseCode). (e.g. StudentName, DateOfBirth, CourseCode).

We use the term RECORD to describe the collection We use the term RECORD to describe the collection of fields which record information about an object of fields which record information about an object

(e.g. a StudentRecord is a collection of fields (e.g. a StudentRecord is a collection of fields recording information about a student).recording information about a student).

We use the term FILE to describe a collection of one We use the term FILE to describe a collection of one or more occurrences (instances) of a record type or more occurrences (instances) of a record type (template).(template).

It is important to distinguish between the record It is important to distinguish between the record occurrence (i.e. the values of a record) and the record occurrence (i.e. the values of a record) and the record type (i.e. the structure of the record). type (i.e. the structure of the record). Every record in a file has a different value but the Every record in a file has a different value but the same structure.same structure.

Page 6: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

Files, Records, Files, Records, Fields.Fields.

StudId StudName DateOfBirthStudId StudName DateOfBirth9723456 COUGHLAN 100919619724567 RYAN 311219769534118 COFFEY 230619649423458 O'BRIEN 031119799312876 SMITH 12121976

StudId StudName DateOfBirthStudId StudName DateOfBirth9723456 COUGHLAN 100919619724567 RYAN 311219769534118 COFFEY 230619649423458 O'BRIEN 031119799312876 SMITH 12121976

STUDENTSSTUDENTS

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

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

occurrencesoccurrences

Record Type Record Type (Template)(Template)(Structure)(Structure)

Page 7: Cobol File Handling

ExampleExample

REGNO NAME AGE

KA101 JYOTHI 19

KA102 ANIRUDH 20

KA103 SRIDHAR 18

Field-1 File Field-2 Field-3

Record-1Record-2Record-3

STUDENT

Page 8: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

Record Record BuffersBuffers

To process a file records are read from the To process a file records are read from the file into the computer’s memory one record file into the computer’s memory one record at a time.at a time.

The computer uses the programmers The computer uses the programmers description of the record (i.e. the record description of the record (i.e. the record template) to set aside sufficient memory to template) to set aside sufficient memory to store one instance of the record.store one instance of the record.

Memory allocated for storing a record is Memory allocated for storing a record is usually called a “record buffer”usually called a “record buffer”

The record buffer is the only connection The record buffer is the only connection between the program and the records in the between the program and the records in the file.file.

Page 9: Cobol File Handling

Record Record BuffersBuffers

IDENTIFICATION DIVISION.etc.ENVIRONMENT DIVISION.etc.DATA DIVISION.FILE SECTION.

ProgramProgram

RecordBufferRecordBuffer DeclarationDeclaration

STUDENTS

DISKRecord Instance

Page 10: Cobol File Handling

Description of a Record buffer for a file containing Single record type

DATA DIVISION.FILE SECTION.FD STUDFILE.01 STUD-REC. 05 REGNO PIC X(5). 05 NAME PIC A(15). 05 AGE PIC 9(2).

Record Buffer and its implicationsRecord Buffer and its implications

Page 11: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

Describing the record buffer in COBOLDescribing the record buffer in COBOL

The record type/template/buffer of The record type/template/buffer of everyevery file used in a program file used in a program mustmust be described in the FILE SECTION by means of an FD be described in the FILE SECTION by means of an FD (file description) entry.(file description) entry.

The FD entry consists of the letters FD and an internal file name.The FD entry consists of the letters FD and an internal file name.

DATA DIVISION.FILE SECTION.FD StudentFile.01 StudentDetails. 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

DATA DIVISION.FILE SECTION.FD StudentFile.01 StudentDetails. 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

Page 12: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

Implications of Implications of ‘Buffers’‘Buffers’

If your program processes more than one file you If your program processes more than one file you will have to describe a record buffer for each file.will have to describe a record buffer for each file.

To process all the records in an INPUT file each To process all the records in an INPUT file each record instance must be copied (read) from the file record instance must be copied (read) from the file into the record buffer when required.into the record buffer when required.

To create an OUTPUT file containing data To create an OUTPUT file containing data records each record must be placed in the record records each record must be placed in the record buffer and then transferred (written) to the file.buffer and then transferred (written) to the file.

To transfer a record from an input file to an To transfer a record from an input file to an output file we will have tooutput file we will have to

read the record into the input record bufferread the record into the input record buffer transfer it to the output record buffertransfer it to the output record buffer write the data to the output file from the write the data to the output file from the

output record bufferoutput record buffer

Page 13: Cobol File Handling

Organization and Organization and AccessAccess

Two important characteristics of files areTwo important characteristics of files are DATA ORGANIZATIONDATA ORGANIZATION METHOD OF ACCESS METHOD OF ACCESS

Data organization refers to the way the records of the file are Data organization refers to the way the records of the file are organized on the backing storage device.organized on the backing storage device.COBOL recognizes three main file organizations;COBOL recognizes three main file organizations;

SequentialSequential - Records organized serially. - Records organized serially. Relative Relative - Relative record number based - Relative record number based

organization.organization. IndexedIndexed - Index based organization.- Index based organization.

The method of access refers to the way in which records are The method of access refers to the way in which records are accessed. accessed.

A file with an organization of Indexed or Relative may A file with an organization of Indexed or Relative may still have its records accessed sequentially. still have its records accessed sequentially.

But records in a file with an organization of Sequential But records in a file with an organization of Sequential can not be accessed directly.can not be accessed directly.

Page 14: Cobol File Handling

Sequential Sequential OrganizationOrganization

The simplest COBOL file organization is Sequential.The simplest COBOL file organization is Sequential.

In a Sequential file the records are arranged serially, one In a Sequential file the records are arranged serially, one after another, like cards in a dealing show. after another, like cards in a dealing show.

In a Sequential file the only way to access any particular In a Sequential file the only way to access any particular record is to; record is to;

Start at the first record and read all the succeeding Start at the first record and read all the succeeding records until you find the one you want or reach the end records until you find the one you want or reach the end of the file.of the file.

Sequential files may be Sequential files may be OrderedOrdered

ororUnorderedUnordered (these should be called Serial files) (these should be called Serial files)

The ordering of the records in a file has a significant impact The ordering of the records in a file has a significant impact on the way in which it is processed and the processing that on the way in which it is processed and the processing that can be done on it.can be done on it.

Page 15: Cobol File Handling

Sequential file organizationSequential file organization

Simplest and least flexible of all types of file Simplest and least flexible of all types of file organizations.organizations.

Can only be accessed sequentially.Can only be accessed sequentially.

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

Does not provide means to insert or delete records.Does not provide means to insert or delete records.

Most storage efficient.Most storage efficient.

Page 16: Cobol File Handling

FILE-CONTROL paragraph for sequential FILE-CONTROL paragraph for sequential filesfiles

SELECT file-name ASSIGN TO implementor-name

[ ORGANIZATION IS SEQUENTIAL ]

[ ACCESS MODE IS SEQUENTIAL]

[ FILE STATUS IS identifier ].

Page 17: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

STUDENTS

The Select and Assign The Select and Assign Clause.Clause.

The internal file name used in the FD entry is connected to an The internal file name used in the FD entry is connected to an external file (on disk or tape) by means of the Select and Assign external file (on disk or tape) by means of the Select and Assign clause.clause.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT StudentFile ASSIGN TO “STUDENTS”.

DATA DIVISION.FILE SECTION.FD StudentFile.01 StudentDetails. 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT StudentFile ASSIGN TO “STUDENTS”.

DATA DIVISION.FILE SECTION.FD StudentFile.01 StudentDetails. 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

DISK

Page 18: Cobol File Handling

File handling verbsFile handling verbs OPENOPEN

READREAD

WRITEWRITE

REWRITEREWRITE

CLOSECLOSE

Page 19: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

COBOL file handling COBOL file handling VerbsVerbs

OPENOPENBefore your program can access the data in an input file or Before your program can access the data in an input file or place data in an output file you must make the file available to place data in an output file you must make the file available to the program by OPENing it.the program by OPENing it.

READREADThe READ copies a record occurrence/instance from the file The READ copies a record occurrence/instance from the file and places it in the record buffer.and places it in the record buffer.

WRITE WRITE The WRITE copies the record it finds in the record buffer to The WRITE copies the record it finds in the record buffer to the file.the file.

CLOSECLOSEYou must ensure that (before terminating) your program You must ensure that (before terminating) your program closes all the files it has opened. Failure to do so may result in closes all the files it has opened. Failure to do so may result in data not being written to the file or users being prevented data not being written to the file or users being prevented from accessing the file.from accessing the file.

Page 20: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

OPEN and CLOSE verb OPEN and CLOSE verb syntaxsyntax

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

Opening a file does not transfer any data to the record Opening a file does not transfer any data to the record buffer, it simply provides access.buffer, it simply provides access.

OPEN InternalFileName ...

INPUT

OUTPUT

EXTEND

Page 21: Cobol File Handling

OPEN verbOPEN verb SyntaxSyntax

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

OPEN MODE

STATEMENT INPUT OUTPUT I-O EXTEND

READ

WRITE

REWRITE

Page 22: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

The READ verb The READ verb

Once the system has opened a file and made it Once the system has opened a file and made it available to the program it is the programmers available to the program it is the programmers responsibility to process it correctly.responsibility to process it correctly.

Remember, the file record buffer is our only Remember, the file record buffer is our only connection with the file and it is only able to connection with the file and it is only able to store a single record at a time.store a single record at a time.

To process all the records in the file we have to To process all the records in the file we have to transfer them, one record at a time, from the file transfer them, one record at a time, from the file to the buffer.to the buffer.

COBOL provides the READ verb for this COBOL provides the READ verb for this purpose.purpose.

Page 23: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

READ verb READ verb syntaxsyntax

The InternalFilename specified must be a file that has The InternalFilename specified must be a file that has been OPENed for INPUT.been OPENed for INPUT.

The NEXT RECORD clause is optional and generally The NEXT RECORD clause is optional and generally not used.not used.

Using INTO Identifier clause causes the data to be Using INTO Identifier clause causes the data to be read into the record buffer and then copied from read into the record buffer and then copied from there to the specified Identifier in one operation.there to the specified Identifier in one operation.

When this option is used there will be two copies When this option is used there will be two copies of the data. It is the equivalent of a READ of the data. It is the equivalent of a READ followed by a MOVE.followed by a MOVE.

READ InternalFilename NEXT RECORD

INTO Identifier

AT END StatementBlock

END - READ

Page 24: Cobol File Handling

Working of the READ Working of the READ statementstatement

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 UNTIL STUD-REC = HIGH-VALUES READ STUDFILE AT END MOVE HIGH-VALUES TO STUD-REC END-READEND-PERFORM.

Page 25: Cobol File Handling

Working of the READ Working of the READ statementstatement

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 UNTIL STUD-REC = HIGH-VALUES READ STUDFILE AT END MOVE HIGH-VALUES TO STUD-REC END-READEND-PERFORM.

Page 26: Cobol File Handling

Working of the READ Working of the READ statementstatement

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 UNTIL STUD-REC = HIGH-VALUES READ STUDFILE AT END MOVE HIGH-VALUES TO STUD-REC END-READEND-PERFORM.

STUD-REC

STUDENT

Page 27: Cobol File Handling

Working of the READ Working of the READ statementstatement

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 UNTIL STUD-REC = HIGH-VALUES READ STUDFILE AT END MOVE HIGH-VALUES TO STUD-REC END-READEND-PERFORM.

STUD-REC

STUDENT

Page 28: Cobol File Handling

Working of the READ Working of the READ statementstatement

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

REGNO NAME AGE

EOF

1 0 2 N I T H Y A 2 2B U

1 0 3 R A C H A N 2 0B U A

PERFORM UNTIL STUD-REC = HIGH-VALUES READ STUDFILE AT END MOVE HIGH-VALUES TO STUD-REC END-READEND-PERFORM.

STUD-REC

STUDENT

Page 29: Cobol File Handling

04/07/2304/07/23 10:10 PM10:10 PM Infosys Technologies LimitedInfosys Technologies Limited

WRITE WRITE Syntax.Syntax.

To WRITE data to a file move the data To WRITE data to a file move the data to the record buffer (declared in the FD to the record buffer (declared in the FD entry) and then WRITEentry) and then WRITE the contents of the contents of record buffer to the file.record buffer to the file.

WRITE

ADVANCING

AdvanceNum

MnemonicName

PAGE

RecordName FROM Identifier

BEFORE

AFTER

LINE

LINES

Page 30: Cobol File Handling

FF rr aa nn kk CC uu rr tt aa ii nn99 33 33 44 55 66 77 LL MM 00 55 11

StudentID StudentName Course.

StudentRecord

FF rr aa nn kk CC uu rr tt aa ii nn99 33 33 44 55 66 77 LL MM 00 55 11

EOF

How the WRITE How the WRITE worksworks

OPEN OUTPUT StudentFile. MOVE "9334567Frank Curtain LM051" TO StudentDetails. WRITE StudentDetails. MOVE "9383715Thomas Healy LM068" TO StudentDetails. WRITE StudentDetails. CLOSE StudentFile. STOP RUN.

OPEN OUTPUT StudentFile. MOVE "9334567Frank Curtain LM051" TO StudentDetails. WRITE StudentDetails. MOVE "9383715Thomas Healy LM068" TO StudentDetails. WRITE StudentDetails. CLOSE StudentFile. STOP RUN.

Students

Page 31: Cobol File Handling

Working of the WRITE statementWorking 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 “BU101JYOTHI 25” TO STUD-REC.WRITE STUD-REC.MOVE “BU102NITHYA 22” TO STUD-REC.WRITE STUD-REC.

B

STUDENT

Page 32: Cobol File Handling

Working of the WRITE statementWorking 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.

Page 33: Cobol File Handling

REWRITE verbREWRITE 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

Page 34: Cobol File Handling

CLOSE verbCLOSE verb

SyntaxSyntax

CLOSECLOSE filename1 filename1

Releases the named files from the program.Releases the named files from the program.

If a file is stored on a magnetic tape, after the If a file is stored on a magnetic tape, after the executionexecution of the CLOSE statement the tape is rewound. of the CLOSE statement the tape is rewound.

Is optional for COBOL- 85.Is optional for COBOL- 85.

Page 35: Cobol File Handling

Sequential filesSequential files - - A Final LookA 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

Page 36: Cobol File Handling

Any Any Questions ????Questions ????

Page 37: Cobol File Handling

Thank youThank you