Download presentation
Presentation is loading. Please wait.
Published byCory Ross Modified over 8 years ago
1
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College “Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department, John Wily & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.”
2
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER 15 Indexed and Relative File Processing
3
Structured COBOL Programming, Stern & Stern, 9th Edition OBJECTIVES To familiarize you with: 1. Methods of disk file organization. 2. Random processing of disk files. 3. How to create, update, and access indexed disk files. 4. How to create, update, and access relative files. 5. Methods used for organizing relative files.
4
Structured COBOL Programming, Stern & Stern, 9th Edition CONTENTS Systems Considerations for Organizing Disk Files –Sequential File Organization –Indexed File Organization –Relative File Organization Features of Magnetic Disks and Disk Drives Processing Indexed Disk Files –Creating an Indexed File –Updating an Indexed File Randomly –Updating an Indexed File with Multiple Transaction Records for Each Master Record
5
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 RELATIVE KEY
6
Structured COBOL Programming, Stern & Stern, 9th Edition Systems Considerations for Organizing Disk Files
7
Structured COBOL Programming, Stern & Stern, 9th Edition Sequential File Organization The simplest type of disk file organization is sequential. Sequential files are processed in the same way regardless of the type of magnetic media on which they are stored. Typically, the records to be stored in a sequential file are first sorted into sequence by a key field such as customer number, part number, or employee number.
8
Structured COBOL Programming, Stern & Stern, 9th Edition Indexed File Organization An indexed file is really two files: – the data file, which is created in sequence but can be accessed randomly, and –the index file, which contains the value of each key field and the disk address of the record with that corresponding key field. To access an indexed record randomly, the key field is looked up in the index file to find the disk address of the record. Then the record is accessed in the indexed data file directly.
9
Structured COBOL Programming, Stern & Stern, 9th Edition Indexed File Organization With an indexed file we can access records either sequentially or randomly, depending on the user's needs. The term random access implies that records are to be processed or accessed in some order other than the one in which they were physically written on the disk.
10
Structured COBOL Programming, Stern & Stern, 9th Edition Relative File Organization Relative files also permits random access. A relative file does not use an index to access records randomly. –Rather, the key field of each record is used to calculate the record's relative location in the file. When records are created as output in a relative file, the key field is used to calculate a disk address where the record is written.
11
Structured COBOL Programming, Stern & Stern, 9th Edition Relative File Organization When records are randomly accessed from a relative file, the user enters the key field, which is used to determine the physical location of the corresponding record. –That record is then accessed directly. Thus there is no need for an index with a relative file.
12
Structured COBOL Programming, Stern & Stern, 9th Edition FEATURES OF MAGNETIC DISKS AND DISK DRIVES
13
Structured COBOL Programming, Stern & Stern, 9th Edition GENERAL FEATURES Magnetic disk is a storage medium that can serve as either input to or output from any computer system - from mainframes to micros. –The disk has a metal oxide coating that can store hundreds of millions of characters of data, or more. The magnetic disk drive, which can be a hard disk drive or a floppy disk drive on the micro, is used both for recording and for reading information from the disk at very high speeds.
14
Structured COBOL Programming, Stern & Stern, 9th Edition ADDRESSING DISK RECORDS Individual records on disks can typically be addressed in the following way: 1. Surface number. 2. Track number. 3. Sector number (for floppy disks) or cylinder number (for larger units).
15
Structured COBOL Programming, Stern & Stern, 9th Edition Processing Indexed Disk Files
16
Structured COBOL Programming, Stern & Stern, 9th Edition Creating an Indexed File Indexed files are created in sequence; – that is, the indexed file is created by reading each record from an input file, in sequence by the key field, and writing the output indexed disk records in the same sequence. Note, however, that once the indexed file is created, it can be accessed randomly.
17
Structured COBOL Programming, Stern & Stern, 9th Edition CREATING AN INDEXED FILE The full SELECT statement for an indexed file is as follows: SELECT file-name-1 ASSIGN TO implementor-name-1 [ORGANIZATION IS] INDEXED [ACCESS MODE IS SEQUENTIAL] RECORD KEY IS data-name-1
18
Structured COBOL Programming, Stern & Stern, 9th Edition The ORGANIZATION Clause The clause ORGANIZATION IS INDEXED indicates that the file is to be created with an index. Even though we are creating the file sequentially, we must indicate that this is an indexed file. This instructs the computer to establish an index so that we can randomly access the file later on.
19
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 or randomly. If the ACCESS clause is omitted, the compiler will assume that the file is being processed in SEQUENTIAL mode. Thus, the ACCESS clause is optional when the file is to be accessed sequentially since ACCESS IS SEQUENTIAL is the default.
20
Structured COBOL Programming, Stern & Stern, 9th Edition The RECORD KEY Clause The RECORD KEY clause names the key field within the disk record that will be used to form the index. This field must be in the same physical location in each indexed record. –Usually, it is the first field. It must have a unique value for each record, and it usually has a numeric value as well.
21
Structured COBOL Programming, Stern & Stern, 9th Edition DEBUGGING TIP: GUIDELINES FOR RECORD KEYS 1. COBOL 85 states that the RECORD KEY should be defined with a PIC of X's. –Most compilers also allow a PIC of 9's as an enhancement. Regardless of whether the record key is defined with X's or 9's, it is best to use a RECORD KEY that has a numeric value.
22
Structured COBOL Programming, Stern & Stern, 9th Edition DEBUGGING TIP: GUIDELINES FOR RECORD KEYS –Fields such as ACCT- NO, SOC-SEC-NO, or PART-NO, for example, make ideal key fields. –Also, different collating sequences (EBCDIC or ASCII) can cause records to be ordered differently in an indexed file if the record key values are not all numeric. 2. We recommend that key fields be the first fields in a record, for ease of reference.
23
Structured COBOL Programming, Stern & Stern, 9th Edition The INVALID KEY Clause The INVALID KEY clause is used with a WRITE instruction to test for two possible errors: 1. A key field that is not in sequence or 2. A key field that is the same as one already in the indexed file. Format: WRITE record-name-1 [FROM identifier-1] [INVALID KEY imperative-statement-1]
24
Structured COBOL Programming, Stern & Stern, 9th Edition Updating an Indexed File Randomly As we have seen, one main feature of disk processing is that master records can be updated directly without having to create a new file. That is, a disk record can be read into storage where changes are made and the changed record can be rewritten back onto the disk in place. –This eliminates the need to create an entirely new file.
25
Structured COBOL Programming, Stern & Stern, 9th Edition Updating an Indexed File Randomly: The SELECT Statement ACCESSING AN INDEXED FILE RANDOMLY SELECT file-name-1 ASSIGN TO implementor-name-1 [ORGANIZATION IS] INDEXED ACCESS MODE IS RANDOM RECORD KEY IS data-name-1
26
Structured COBOL Programming, Stern & Stern, 9th Edition Updating an Indexed File Randomly Opening an Indexed File as I-O When updating an indexed file, we open it 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 reading or accessing disk records (2) it is also used as output [O] for rewriting or updating the records read.
27
Structured COBOL Programming, Stern & Stern, 9th Edition Updating an Indexed File Randomly: The READ Statement A COBOL 85 sample READ: READ INDEXED-FILE INVALID KEY PERFORM 600-ERR-RTN NOT INVALID KEY PERFORM 500-OK-RTN END-READ
28
Structured COBOL Programming, Stern & Stern, 9th Edition There is No AT END Clause When Reading from a Disk Randomly When reading a disk file randomly, we do not test for an AT END condition because we are not reading the file in sequence; 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 will be executed. –Thus, the computer executes the INVALID KEY option only if the T-PART-NO does not match any of the master disk records. NOT INVALID KEY is COBOL 85 only.
29
Structured COBOL Programming, Stern & Stern, 9th Edition Updating an Indexed Master File For updating an indexed file, we have the following: 1. OPEN the indexed master file as I-O. 2. READ transaction data from a transaction file or accept transaction data 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 master file. When a READ (master file) instruction is executed, the indexed master file record with that RECORD KEY will be transmitted to the master record storage area in the FILE SECTION.
30
Structured COBOL Programming, Stern & Stern, 9th Edition Updating an Indexed Master File 3. When the READ (master file) instruction is executed, the corresponding master record that needs to be updated will be read into main memory. 4. Make the changes to the master record directly by moving transaction data to the master I/O record area. 5. REWRITE the master record.
31
Structured COBOL Programming, Stern & Stern, 9th Edition Format for the REWRITE to Update an Indexed Master File REWRITE record-name-1 [FROM identifier-1] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative- statement-2]* [END-REWRITE]* *These are optional with COBOL 85 only.
32
Structured COBOL Programming, Stern & Stern, 9th Edition Additional Features of an Update Procedure: The Format for the DELETE DELETE 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.
33
Structured COBOL Programming, Stern & Stern, 9th Edition QUESTIONS?
34
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 1. To access records in an indexed file randomly, we move the transaction record's key field to the ____. SOLUTION: RECORD KEY of the indexed record
35
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 2. When a record is to be deleted from an indexed file, we use a ____ instruction. SOLUTION: DELETE file-name
36
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 3. The INVALID KEY option can be part of which statements? SOLUTION: The READ (where ACCESS IS RANDOM is specified), WRITE, REWRITE, or DELETE
37
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 4. The INVALID KEY option tests the validity of the ____ KEY. SOLUTION: RECORD
38
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 5. If READ FILE-X INVALID KEY PERFORM 800-ERROR-1 is executed, 800-ERROR-1 will be performed if ____. SOLUTION: a record with the indicated RECORD KEY cannot be found in FILE-X
39
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 6. (T or F) Indexed files are typically created in sequence by RECORD KEY. SOLUTION: T
40
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 7. If a record is to be added to a disk file, a (WRITE, REWRITE) statement is used. SOLUTION: WRITE
41
Structured COBOL Programming, Stern & Stern, 9th Edition Processing Relative Disk Files
42
Structured COBOL Programming, Stern & Stern, 9th Edition What Is a Relative File? With indexed files, the key fields of records to be accessed are looked up in an index to find the disk address. With relative files, the key field is converted to an actual disk address so that there is no need for an index or for a search to find the location of a record.
43
Structured COBOL Programming, Stern & Stern, 9th Edition What Is a Relative File? FORMAT SELECT file-name-1 ASSIGN TO implementor-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]
44
Structured COBOL Programming, Stern & Stern, 9th Edition Creating Relative Files Relative files are created sequentially, and either the computer or the user can supply the key. When a relative file's SELECT statement includes ACCESS IS SEQUENTIAL, the RELATIVE KEY clause can be omitted. –If the RELATIVE KEY clause is omitted, the computer writes the records with keys designated as 1 to n. That is, the first record is placed in relative record location 1 (RELATIVE KEY = 1), the second in relative record location 2 (RELATIVE KEY = 2), and so on.
45
Structured COBOL Programming, Stern & Stern, 9th Edition Sequential Reading of Relative Files The records in relative files may be read sequentially, that is, in the order that they were created. Because a relative file is created in sequence by RELATIVE KEY, a sequential READ reads the records in ascending relative key order. There is no need to specify a RELATIVE KEY for reading from a relative file sequentially.
46
Structured COBOL Programming, Stern & Stern, 9th Edition Random Updating of Relative Files When updating a relative file, you can access each record to be changed and REWRITE it directly. The relative file must be opened as I-O, the required record must be read, changed, and then rewritten for each update.
47
Structured COBOL Programming, Stern & Stern, 9th Edition MORE QUESTIONS?
48
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 1. When creating a relative file, ACCESS IS ____. When using a relative file as input, ACCESS IS either ____ or ____. SOLUTION: SEQUENTIAL; SEQUENTIAL; RANDOM (or DYNAMIC)
49
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 2. RELATIVE KEY is optional when reading or writing a relative file (sequentially, randomly). SOLUTION: sequentially
50
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 3. (T or F) If ACCT-NO is used to calculate a disk address when writing records on a relative file, then ACCT-NO must be moved to a WORKING-STORAGE entry designated as the RELATIVE KEY before a WRITE is executed. SOLUTION: T
51
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 4. (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 a WORKING-STORAGE entry specified in the RELATIVE KEY clause of the SELECT statement or converted to the WORKING- STORAGE RELATIVE KEY, as described in the next section.
52
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 5. (T or F) Relative file organization is the most popular method for organizing a disk file that may be accessed randomly. SOLUTION: F - Indexed file organization is the most popular.
53
Structured COBOL Programming, Stern & Stern, 9th Edition CONVERTING A KEY FIELD TO A RELATIVE KEY: CLAUSES TO UPDATE RANDOM-ACCESS FILES OPEN I-O – Used when a relative file isbeing updated. REWRITE –Writes back onto a relative file (you can only use REWRITE when the file is opened as I-O and a record has already been read from it). INVALID KEY –Is required with relative (and indexed) files for a random READ and any WRITE, DELETE, and REWRITE unless a USE AFTER STANDARD EXCEPTION declarative has been specified.
54
Structured COBOL Programming, Stern & Stern, 9th Edition CONVERTING A KEY FIELD TO A RELATIVE KEY: CLAUSES TO UPDATE RANDOM-ACCESS FILES INVALID KEY –The computer will perform the statements following the INVALID KEY if the record cannot be found or if the RELATIVE KEY is blank or not numeric. A NOT INVALID KEY clause may be used with COBOL 85. DELETE –Eliminates records from the file.
55
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SLIDES END HERE CHAPTER SUMMARY COMES NEXT
56
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY I. Indexed File Processing A. What is an Indexed File? 1. ENVIRONMENT DIVISION - SELECT clause specifies: ORGANIZATION IS INDEXED ACCESS IS RANDOM - For nonsequential updates, inquiries, etc. SEQUENTIAL - For creating an indexed file, reporting from it in sequence, and updating it sequentially. RECORD KEY - This is the key field in each indexed disk record that is used for establishing the index and for accessing disk records.
57
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY 2. DATA DIVISION a. LABEL RECORDS are usually STANDARD for all disk files. b. Records are usually blocked. 3. PROCEDURE DIVISION a. Creating an indexed file (1) Indexed files are created with an ACCESS IS SEQUENTIAL clause in the ENVIRONMENT DIVISION. (2) The WRITE statement should include the INVALID KEY clause. The statement following INVALID KEY is executed (1) if a record with the same key was already created, (2) if the record is out of sequence or, (3) on many systems, if the key is blank. b. Reading from an indexed file - in sequence (1) Same as all sequential processing. (2) Use READ... AT END.
58
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY II. Relative File Processing A. What is a Relative File? 1. Relative files, like indexed files, can be accessed randomly. 2. With a relative file, there is no index. Instead, a record's key field such as ACCT-NO is converted to a relative record number or RELATIVE KEY. The conversion can be one-to- one (RELATIVE KEY = record key), or a randomizing algorithm may be used to calculate a relative record number from a record's key field.
59
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY 3. The random accessing of a relative file is very fast because there is no need to look up a disk address from an index. 4. Sequential access of a relative file may be slow because records adjacent to one another in the file do not necessarily have key fields in sequence. B. Processing Relative Files 1. SELECT statement. a. Code ORGANIZATION IS RELATIVE
60
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY b. RELATIVE KEY clause uses (1) For randomly accessing the file. (2) For sequential reads and writes if a conversion is necessary from a record's key field to a RELATIVE KEY. (3) The data-name used as the relative record number or RELATIVE KEY is defined in WORKING-STORAGE. c. ACCESS can be SEQUENTIAL, RANDOM, or DYNAMIC. DYNAMIC means the file is accessed both randomly and sequentially in the same program.
61
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER SUMMARY 2. Processing routines. a. Creating a relative file: (1) ACCESS IS SEQUENTIAL in the SELECT statement. (2) Move the input record's key field to the RELATIVE KEY, which is in WORKING-STORAGE (or convert the input key to a WORKING- STORAGE relative key) and WRITE... INVALID KEY b. Accessing a relative file randomly: (1) ACCESS IS RANDOM in the SELECT statement. (2) Move the transaction record's key field to the RELATIVE KEY, which is in WORKING-STORAGE (or convert) and READ... INVALID KEY.... c. When updating a relative file, open it as I-O, ACCESS IS RANDOM, and use READ, WRITE, REWRITE, or DELETE with INVALID KEY clauses.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.