Nancy Stern Hofstra University Robert A. Stern: Nassau Community College
Nancy Stern Hofstra University Robert A. Stern: Nassau Community College
Copyright @ 2000 John Wil ey & 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 unl awful. Request for further information should be addressed to the permissions Department , John Wil y & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resal e. 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.
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.
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
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 Structured COBOL Programming, Stern & Stern, 9th Transaction Records for Edition Each Master Record
CONTENTS
Accessing or Reading from an Indexed File for Reporting Purposes The FILE STATUS Clause
Converting a Key Field to a RELATIVE Structured KEY COBOL Programming, Stern & Stern, 9th Edition
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 & Stern, 9th Edition directly. Structured COBOL Programming, Stern
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.
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.
Structured COBOL Programming, Stern & Stern, 9th Edition
Note, however, that once the indexed file is created, it can be accessed randomly.
Structured COBOL Programming, Stern & Stern, 9th Edition
It must have a unique value for each record, and it usually has a numeric value as well.
Structured COBOL Programming, Stern & Stern, 9th Edition
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.
2. We recommend that key fields be the first fields in a record, for ease of reference.
Structured COBOL Programming, Stern & Stern, 9th Edition
READ INDEXED-FILE INVALID KEY PERFORM 600-ERR-RTN NOT INVALID KEY PERFORM 500-OK-RTN END-READ
QUESTIONS?
SELF-TEST
1. To access records in an indexed file randomly, we move the transaction record's key field to the ____ .
SELF-TEST
2. When a record is to be deleted from an indexed file, we use a ____ instruction.
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
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
4. The INVALID KEY option tests the validity of the ____ KEY.
SOLUTION: RECORD
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
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
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
Structured COBOL Programming, Stern & Stern, 9th Edition
MORE QUESTIONS?
SELF-TEST
1. When creating a relative file, ACCESS IS ____ . When using a relative file as input, ACCESS IS either ____ or ____ .
SELF-TEST
2. RELATIVE KEY is optional when reading or writing a relative file (sequentially, randomly).
SOLUTION: sequentially
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
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
4. (T or F) To read the record with CUSTNO 125, move 125 to the record's CUSTNO 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.
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.
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, Programming, Stern & Stern, 9th Edition Structured COBOL and REWRITE unless a USE AFTER
DELETE
Eliminates records from the file.
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.
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 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 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
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.
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 Structured COBOL Programming, Stern & Stern, 9th Edition SELECT statement.