8/4/2019 ch15_97
1/61
PowerPoint Presentation:Richard H. Baum, Ph.D.DeVry Institute of Technology
9th Edition
Structured COBOLProgrammingNancy Stern
Hofstra UniversityRobert A. Stern
Nassau CommunityCollege
Copyright @ 2000 John Wiley & Sons, In. Allr ights reserved. Reproduction or translation of thiswork beyond that permitted in Section 117 of the1976 United States Copyright Act without theexpress permission of the copyright owner isunlawful. Request for further information should beaddressed to the permissions Department , JohnWily & Sons, Inc. The purchaser may make back-upcopies for his/her own use only and not fordistribution or resale. The Publisher assumes noresponsibility for errors, omissi ons, or damages,caused by the use of these programs or from th euse of the information contained herein.
8/4/2019 ch15_97
2/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER 15Indexed and RelativeFile Processing
8/4/2019 ch15_97
3/61
Structured COBOL Programming, Stern & Stern, 9th Edition
OBJECTIVESTo familiarize you with:
1. Methods of disk file organization.2. Random processing of disk files.3. How to create, update, and accessindexed disk files.4. How to create, update, and accessrelative files.5. Methods used for organizing relativefiles.
8/4/2019 ch15_97
4/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS Systems Considerations for OrganizingDisk Files
Sequential File Organization Indexed File Organization Relative File Organization
Features of Magnetic Disks and DiskDrives Processing Indexed Disk Files
Creating an Indexed File Updating an Indexed File Randomly Updating an Indexed File with MultipleTransaction Records for Each Master Record
8/4/2019 ch15_97
5/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS Accessing or Reading from an Indexed File
for Reporting Purposes The FILE STATUS Clause
Processing Relative Disk Files What is a Relative File? Creating Relative Files Sequential Reading of Relative Files Random Reading of Relative Files Random Updating of Relative Files
Converting a Key Field to a RELATIVEKEY
8/4/2019 ch15_97
6/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Systems Considerations forOrganizing Disk Files
8/4/2019 ch15_97
7/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Sequential File Organization The simplest type of disk fileorganization is sequential. Sequential files are processed in the
same way regardless of the type ofmagnetic media on which they arestored. Typically, the records to be stored in asequential file are first sorted intosequence by a key field such ascustomer number, part number, oremployee number.
8/4/2019 ch15_97
8/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Indexed File Organization An indexed file is really two files:
the data file, which is created in sequencebut can be accessed randomly, and the index file, which contains the value ofeach key field and the disk address of therecord with that corresponding key field.
To access an indexed record randomly,the key field is looked up in the indexfile to find the disk address of therecord.
Then the record is accessed in theindexed data file directly.
8/4/2019 ch15_97
9/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Indexed File Organization With an indexed file we can accessrecords either sequentially or randomly,depending on the user's needs. The term random access implies thatrecords are to be processed or accessedin some order other than the one in
which they were physically written onthe disk.
8/4/2019 ch15_97
10/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Relative File Organization Relative files also permits random
access. A relative file does not use an index toaccess records randomly.
Rather, the key field of each record is usedto calculate the record's relative location inthe file. When records are created as output in arelative file, the key field is used tocalculate a disk address where the
record is written.
8/4/2019 ch15_97
11/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Relative File Organization When records are randomly accessedfrom a relative file, the user enters thekey field, which is used to determine the
physical location of the correspondingrecord. That record is then accessed directly.
Thus there is no need for an index witha relative file.
8/4/2019 ch15_97
12/61
Structured COBOL Programming, Stern & Stern, 9th Edition
FEATURES OF MAGNETICDISKS AND DISK DRIVES
8/4/2019 ch15_97
13/61
Structured COBOL Programming, Stern & Stern, 9th Edition
GENERAL FEATURES Magnetic disk is a storage medium thatcan serve as either input to or outputfrom any computer system - frommainframes to micros.
The disk has a metal oxide coating that canstore hundreds of mill ions of characters ofdata, or more. The magnetic disk drive, which can be a
hard disk drive or a floppy disk drive onthe micro, is used both for recording andfor reading information from the disk atvery high speeds.
8/4/2019 ch15_97
14/61
Structured COBOL Programming, Stern & Stern, 9th Edition
ADDRESSING DISK RECORDSIndividual records on disks can typicallybe addressed in the following way:
1. Surface number.2. Track number.3. Sector number (for floppy disks) orcylinder number (for larger units).
8/4/2019 ch15_97
15/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Processing Indexed DiskFiles
8/4/2019 ch15_97
16/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Creating an Indexed File Indexed files are created in sequence;
that is, the indexed file is created byreading each record from an input fi le, insequence by the key field, and writing theoutput indexed disk records in the samesequence.
Note, however, that once the indexedfile is created, it can be accessedrandomly.
8/4/2019 ch15_97
17/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CREATING AN INDEXED FILEThe full SELECT statement for an indexedfile is as follows:SELECT file-name-1 ASSIGN TOimplementor-name-1
[ORGANIZATION IS] INDEXED[ACCESS MODE IS SEQUENTIAL]
RECORD KEY IS data-name-1
8/4/2019 ch15_97
18/61
Structured COBOL Programming, Stern & Stern, 9th Edition
The ORGANIZATION Clause The clause ORGANIZATION IS INDEXEDindicates that the file is to be createdwith an index. Even though we are creating the filesequentially, we must indicate that thisis an indexed file. This instructs the computer to establishan index so that we can randomly accessthe file later on.
8/4/2019 ch15_97
19/61
Structured COBOL Programming, Stern & Stern, 9th Edition
The ACCESS Clause The ACCESS clause is used to denote if
the file is to be accessed sequentially orrandomly. If the ACCESS clause is omitted, the
compiler will assume that the file isbeing processed in SEQUENTIAL mode. Thus, the ACCESS clause is optionalwhen the file is to be accessedsequentially since ACCESS ISSEQUENTIAL is the default.
8/4/2019 ch15_97
20/61
Structured COBOL Programming, Stern & Stern, 9th Edition
The RECORD KEY Clause The RECORD KEY clause names the keyfield within the disk record that will beused to form the index. This field must be in the same physicallocation in each indexed record.
Usually, it is the first field. It must have a unique value for eachrecord, and it usually has a numericvalue as well.
8/4/2019 ch15_97
21/61
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP: GUIDELINESFOR RECORD KEYS1. COBOL 85 states that the RECORD KEYshould be defined with a PIC of X's.
Most compilers also allow a PIC of 9's as anenhancement. Regardless of whether the record key isdefined with X's or 9's, it is best to usea RECORD KEY that has a numeric value.
8/4/2019 ch15_97
22/61
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP: GUIDELINESFOR RECORD KEYS Fields such as ACCT- NO, SOC-SEC-NO, orPART-NO , for example, make ideal keyfields. Also, different collating sequences (EBCDICor ASCII) can cause records to be ordereddifferently in an indexed file if the recordkey values are not all numeric.
2. We recommend that key fields be thefirst fields in a record, for ease ofreference.
8/4/2019 ch15_97
23/61
Structured COBOL Programming, Stern & Stern, 9th Edition
The INVALID KEY Clause The INVALID KEY clause is used with aWRITE instruction to test for twopossible errors:
1. A key field that is not in sequence or2. A key field that is the same as onealready in the indexed fi le.
Format:WRITE record-name-1 [FROM identifier-1]
[INVALID KEY imperative-statement-
8/4/2019 ch15_97
24/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Updating an Indexed FileRandomly As we have seen, one main feature ofdisk processing is that master recordscan be updated directly without having
to create a new file. That is, a disk record can be read intostorage where changes are made and the
changed record can be rewritten backonto the disk in place. This eliminates the need to create anentirely new file.
8/4/2019 ch15_97
25/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Updating an Indexed FileRandomly: The SELECTStatementACCESSING AN INDEXED FILE RANDOMLYSELECT file-name-1 ASSIGN TO
implementor-name-1[ORGANIZATION IS] INDEXED
ACCESS MODE IS RANDOMRECORD KEY IS data-name-1
8/4/2019 ch15_97
26/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Updating an Indexed FileRandomly Opening an IndexedFile as I-O When updating an indexed file, we openit as I-O, for input-output, because it
will be read from and written to. That is,(1) it is used as input [I] for readingor accessing disk records(2) it is also used as output [O] forrewriting or updating the records read.
8/4/2019 ch15_97
27/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Updating an Indexed FileRandomly: The READ Statement A COBOL 85 sample READ:READ INDEXED-FILE INVALID KEY PERFORM 600-ERR-RTNNOT INVALID KEY PERFORM 500-OK-RTNEND-READ
8/4/2019 ch15_97
28/61
Structured COBOL Programming, Stern & Stern, 9th Edition
There is No AT END Clause WhenReading from a Disk Randomly When reading a disk file randomly, wedo not test for an AT END conditionbecause we are not reading the file insequence; instead, we include an
INVALID KEY test. If there is no record in the INDEXED-FILE with a RECORD KEY equal to T-
PART-NO, the INVALID KEY clause willbe executed. Thus, the computer executes the INVALIDKEY option only if the T-PART-NO does notmatch any of the master disk records.
8/4/2019 ch15_97
29/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Updating an IndexedMaster File For updating an indexed file, we havethe following:
1. OPEN the indexed master file as I-O.2. READ transaction data from atransaction file or accept transactiondata from a keyboard.
Move the key field for the transaction record,which was either read in or accepted as input,to the RECORD KEY of the indexed masterfile. When a READ (master file) instruction isexecuted, the indexed master file record withthat RECORD KEY will be transmit ted to themaster record storage area in the FILE
8/4/2019 ch15_97
30/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Updating an IndexedMaster File3. When the READ (master file)instruction is executed, thecorresponding master record that
needs to be updated will be read intomain memory.4. Make the changes to the master
record directly by moving transactiondata to the master I/O record area.5. REWRITE the master record.
8/4/2019 ch15_97
31/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Update an Indexed MasterFile REWRITE record-name-1 [FROMidentifier-1]
[INVALID KEY imperative-statement-1][NOT INVALID KEY imperative-
statement-2]*[END-REWRITE]**These are optional with COBOL 85 only.
8/4/2019 ch15_97
32/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Additional Features of anUpdate Procedure: TheFormat for the DELETEDELETE indexed-file-name-1 RECORD
[INVALID KEY imperative-statement-1][NOT INVALID KEY imperative-statement- 2]*
[END-DELETE]** These are optional with COBOL 85 only.
8/4/2019 ch15_97
33/61
Structured COBOL Programming, Stern & Stern, 9th Edition
QUESTIONS?
8/4/2019 ch15_97
34/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST1. To access records in an indexed filerandomly, we move the transactionrecord's key field to the ____ .
SOLUTION: RECORD KEY of the indexedrecord
8/4/2019 ch15_97
35/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST2. When a record is to be deleted from anindexed file, we use a ____ instruction.
SOLUTION: DELETE file-name
8/4/2019 ch15_97
36/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST3. The INVALID KEY option can be part ofwhich statements?
SOLUTION: The READ (where ACCESS ISRANDOM is specified), WRITE, REWRITE,or DELETE
8/4/2019 ch15_97
37/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST4. The INVALID KEY option tests thevalidity of the ____ KEY.
SOLUTION: RECORD
8/4/2019 ch15_97
38/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST5. If READ FILE-X INVALID KEY PERFORM800-ERROR-1 is executed, 800-ERROR-1will be performed if ____ .
SOLUTION: a record with the indicatedRECORD KEY cannot be found in FILE-X
8/4/2019 ch15_97
39/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST6. (T or F) Indexed files are typicallycreated in sequence by RECORD KEY.
SOLUTION: T
8/4/2019 ch15_97
40/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST7. If a record is to be added to a disk file,a (WRITE, REWRITE) statement is used.
SOLUTION: WRITE
8/4/2019 ch15_97
41/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Processing Relative DiskFiles
8/4/2019 ch15_97
42/61
Structured COBOL Programming, Stern & Stern, 9th Edition
What Is a Relative File? With indexed files, the key fields ofrecords to be accessed are looked up inan index to find the disk address. With relative files, the key field isconverted to an actual disk address sothat there is no need for an index or for
a search to find the location of a record.
8/4/2019 ch15_97
43/61
Structured COBOL Programming, Stern & Stern, 9th Edition
What Is a Relative File? FORMATSELECT file-name-1 ASSIGN TOimplementor-name-1
[ORGANIZATION IS] RELATIVE[ACCESS IS {SEQUENTIAL
[RELATIVE KEY IS data- name-1]}] {RANDOM} {DYNAMIC}
RELATIVE KEY IS data-name-1}][FILE STATUS IS data-name-2]
8/4/2019 ch15_97
44/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Creating Relative Files Relative files are created sequentially,
and either the computer or the user cansupply the key. When a relative file's SELECT statement
includes ACCESS IS SEQUENTIAL, theRELATIVE KEY clause can be omitted. If the RELATIVE KEY clause is omitted, thecomputer writes the records with keys
designated as 1 to n. That is, the first record is placed in relativerecord location 1 (RELATIVE KEY = 1), thesecond in relative record location 2(RELATIVE KEY = 2), and so on.
8/4/2019 ch15_97
45/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Sequential Reading ofRelative Files The records in relative files may be readsequentially, that is, in the order thatthey were created. Because a relative file is created insequence by RELATIVE KEY, a sequentialREAD reads the records in ascendingrelative key order. There is no need to specify a RELATIVEKEY for reading from a relative filesequentially.
8/4/2019 ch15_97
46/61
Structured COBOL Programming, Stern & Stern, 9th Edition
Random Updating of RelativeFiles When updating a relative file, you canaccess each record to be changed andREWRITE it directly. The relative file must be opened as I-O,the required record must be read,changed, and then rewritten for each
update.
8/4/2019 ch15_97
47/61
Structured COBOL Programming, Stern & Stern, 9th Edition
MORE QUESTIONS?
8/4/2019 ch15_97
48/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST1. When creating a relative file, ACCESS IS____ . When using a relative file asinput, ACCESS IS either ____ or ____ .
SOLUTION: SEQUENTIAL; SEQUENTIAL;RANDOM (or DYNAMIC)
8/4/2019 ch15_97
49/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST2. RELATIVE KEY is optional when readingor writing a relative file (sequentially,randomly).
SOLUTION: sequentially
8/4/2019 ch15_97
50/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST3. (T or F) If ACCT-NO is used to calculatea disk address when writing records on arelative file, then ACCT-NO must be
moved to a WORKING-STORAGE entrydesignated as the RELATIVE KEY beforea WRITE is executed.
SOLUTION: T
8/4/2019 ch15_97
51/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST4. (T or F) To read the record with CUST-
NO 125, move 125 to the record's CUST-NO key field and execute a READ.
SOLUTION: F - 125 must be moved to aWORKING-STORAGE entry specified inthe RELATIVE KEY clause of the SELECTstatement or converted to theWORKING-STORAGE RELATIVE KEY, asdescribed in the next section.
8/4/2019 ch15_97
52/61
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST5. (T or F) Relative file organization is themost popular method for organizing adisk file that may be accessed randomly.
SOLUTION: F - Indexed file organization isthe most popular.
CONVERTING A KEY FIELD TO A
8/4/2019 ch15_97
53/61
Structured COBOL Programming, Stern & Stern, 9th Edition
RELATIVE KEY: CLAUSES TOUPDATE RANDOM-ACCESS FILESOPEN I-O
Used when a relative fi le is beingupdated.REWRITE
Writes back onto a relative fi le (you canonly use REWRITE when the file is openedas I-O and a record has al ready been readfrom it).INVALID KEY
Is required with relative (and indexed) fi lesfor a random READ and any WRITE,DELETE, and REWRITE unless a USE AFTER
8/4/2019 ch15_97
54/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CONVERTING A KEY FIELD TO ARELATIVE KEY: CLAUSES TOUPDATE RANDOM-ACCESS FILESINVALID KEY
The computer will perform the statementsfollowing the INVALID KEY if the recordcannot be found or if the RELATIVE KEY isblank or not numeric. A NOT INVALID KEYclause may be used with COBOL 85.DELETE
Eliminates records from the file.
8/4/2019 ch15_97
55/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SLIDES END HERECHAPTER SUMMARY COMES NEXT
8/4/2019 ch15_97
56/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARYI. Indexed File ProcessingA. What is an Indexed File?1. ENVIRONMENT DIVISION - SELECTclause specifies:
ORGANIZATION IS INDEXEDACCESS IS RANDOM - Fornonsequential updates, inquiries,etc.
SEQUENTIAL - Forcreating an indexed file, reportingfrom it in sequence, and updating itsequentially.
8/4/2019 ch15_97
57/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY2. DATA DIVISIONa. LABEL RECORDS are usuallySTANDARD for all disk files.b. Records are usually blocked.3. PROCEDURE DIVISION
a. Creating an indexed file(1) Indexed files are created withan ACCESS IS SEQUENTIAL clausein the ENVIRONMENT DIVISION.(2) The WRITE statement shouldinclude the INVALID KEY clause.The statement following INVALIDKEY is executed (1) if a record
8/4/2019 ch15_97
58/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARYII. Relative File ProcessingA. What is a Relative File?
1. Relative files, like indexed files, canbe accessed randomly.2. With a relative file, there is no index.Instead, a record's key field such asACCT-NO is converted to a relative
record number or RELATIVE KEY. Theconversion can be one-to-one(RELATIVE KEY = record key), or arandomizing algorithm may be used to
8/4/2019 ch15_97
59/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY3. The random accessing of a relativefile is very fast because there is noneed to look up a disk address from anindex.4. Sequential access of a relative filemay be slow because records adjacentto one another in the file do not
necessarily have key fields insequence.B. Processing Relative Files
8/4/2019 ch15_97
60/61
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARYb. RELATIVE KEY clause uses
(1) For randomly accessing the file.(2) For sequential reads and writesif a conversion is necessary from arecord's key field to a RELATIVEKEY.(3) The data-name used as therelative record number orRELATIVE KEY is defined inWORKING-STORAGE.
8/4/2019 ch15_97
61/61
CHAPTER SUMMARY2. Processing routines.a. Creating a relative file:
(1) ACCESS IS SEQUENTIAL in theSELECT statement.(2) Move the input record's keyfield to the RELATIVE KEY, whichis in WORKING-STORAGE (orconvert the input key to aWORKING- STORAGE relative key)and WRITE ... INVALID KEY
b. Accessing a relative file randomly: