Title: RANDOM FILE ACCESS
1RANDOM FILE ACCESS
2Computer Structure
The transfer of information from the CPU to Main
Memory is quick (gt 10-9 seconds) An executing
program must have its code in Main Memory (not
necessary all the code, but the piece which is
being executed) Main Memory is not
permanent Permanent data stored on files READ
STUDENT-REC provides a copy of the record on the
file on disk into the buffer of the
program reading from disk is long (about 10 -3
seconds) READ or WRITE is called an I-O operation
3Indexed Files
Hypothetical Seneca College Student Data
File NWH 12340 JONES BOBBY CBL344 NWH
12341 DOUGLAS MICHAEL DBL526 NWH 12342
CARTER STEVEN DBS329 NWH 12345 FISHER
JENNY HUM100 NWH 12348 XU PAULINE
MAT241 YRK 12365 JORDAN GEOFF PHY101 YRK
12378 LI TONY BIO233 YRK 12444 YIN
PAUL CHM332 YRK 12699 MATHEWS PAMELA
ENG101 YRK 23333 SMITH JOHN FRE232
- So far we have limited data retrieval from files
to sequential reads only. This is good for batch
processing, when a large number of records are to
be processed. - But in an online system, the user may just need
to get at one record at a time (e.g. you make
want to drop a course, so the registrar will
retrieve your student record and update it.) - Online Systems therefore have indexed files.
- An indexed file is really two files
- 1.) a data file in a key sequence, and
- 2.) the index, which contains the value of each
keyed field and the disk address of the record
with that corresponding key field
First Name
Course Code
Campus Code
First Name
Student Number
Last Name
4Indexed Files
- Indexed Files
- Can read/write a particular record directly (do
not have to start reading at the top.) - Can also be read sequentially
- Must have a key in sequence
- Key must be unique (unless DUPLICATES)
- Example In the Student Data file opposite, the
key is the Campus code the student number.
Hypothetical Seneca College Student Data
File NWH 12340 JONES BOBBY CBL344 NWH
12341 DOUGLAS MICHAEL DBL526 NWH 12342
CARTER STEVEN DBS329 NWH 12345 FISHER
JENNY HUM100 NWH 12348 XU PAULINE
MAT241 YRK 12365 JORDAN GEOFF PHY101 YRK
12378 LI TONY BIO233 YRK 12444 YIN
PAUL CHM332 YRK 12699 MATHEWS PAMELA
ENG101 YRK 23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
5Accessing an Indexed File Randomly - SELECT
- SELECT Statement
- Example
- SELECT STUDENT-FILE
- ASSIGN TO DISK-SNCFLE
- ORGANIZATION IS INDEXED
- ACCESS IS RANDOM
- RECORD KEY IS STUDENT-KEY
- FILE STATUS IS WS-STUDENT-FLE-ST.
- ORGANIZATION
- INDEXED - file is in sequence, and has an index
based upon this key
Hypothetical Seneca College Student Data
File NWH 12340 JONES BOBBY CBL344 NWH
12341 DOUGLAS MICHAEL DBL526 NWH 12342
CARTER STEVEN DBS329 NWH 12345 FISHER
JENNY HUM100 NWH 12348 XU PAULINE
MAT241 YRK 12365 JORDAN GEOFF PHY101 YRK
12378 LI TONY BIO233 YRK 12444 YIN
PAUL CHM332 YRK 12699 MATHEWS PAMELA
ENG101 YRK 23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
6Accessing an Indexed File Randomly - SELECT
Common File Status Codes (page 484) Digit 1st
2nd Meaning 0 0 Successful operation 1 0
End of file 2 0 Invalid key 2 1
Invalid key, data out of sequence 2 2
Invalid key, duplicate key found 2 3
Invalid key, no record found 2 4 Invalid
key, boundary violation 3 0 Permanent I-O
error, cause unknown 9 0 Other error, no
further information 9 2 Logic error
- SELECT Statement
- ACCESS
- SEQUENTIAL - Only sequential reading of the file
is to be done - RANDOM - Only keyed reads are to be done (one
record retrieval) - DYNAMIC - Both sequential and random access are
to be done - RECORD KEY
- Specifies the key of the file
- Always required
- FILE STATUS
- A WORKING-STORAGE variable of two bytes in
length. After all I-O operations, the AS400 O/S
will place a two byte return code here.
7Accessing an Indexed File Randomly - READ
- READ Statement
- SYNTAX
- READ file-name-1 INTO identifier-1
- INVALID KEY imperative-statement-1
- NOT INVALID KEY imperative-statement-2
- END-READ
- Data must first be placed into the RECORD KEY.
- If ACCESS is RANDOM, the READ statement will read
the record that correspondS to the data
previously placed in the RECORD KEY - If the key is found, the NOT INVALID KEY cause
becomes true - Otherwise INVALID KEY clause is TRUE
Hypothetical Seneca College Student Data
File NWH 12340 JONES BOBBY CBL344 NWH
12341 DOUGLAS MICHAEL DBL526 NWH 12342
CARTER STEVEN DBS329 NWH 12345 FISHER
JENNY HUM100 NWH 12348 XU PAULINE
MAT241 YRK 12365 JORDAN GEOFF PHY101 YRK
12378 LI TONY BIO233 YRK 12444 YIN
PAUL CHM332 YRK 12699 MATHEWS PAMELA
ENG101 YRK 23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
8Random READ of a Record - READ
- Example Read Pauline Xus student file record
- DATA DIVISION.
- FILE SECTION.
- FD STUDENT-FILE.
- 01 STUDENT-FILE-REC.
- 05 STUDENT-KEY.
- 10 CAMPUS PIC X(03).
- 88 NEWHAM VALUE NWH.
- 88 SENECA-AT-YORK VALUE YRK.
- 10 STUDENT-NUM PIC 9(05).
- 05 STUDENT-FNAM PIC X(10).
- 05 STUDENT-LNAM PIC X(10).
- 05 COURSE PIC X(06).
-
- PROCEDURE DIVISION.
- SET NEWHAM TO TRUE
- MOVE 12348 TO STUDENT-NUM
- READ STUDENT-FILE
Hypothetical Seneca College Student Data
File NWH 12340 JONES BOBBY CBL344 NWH
12341 DOUGLAS MICHAEL DBL526 NWH 12342
CARTER STEVEN DBS329 NWH 12345 FISHER
JENNY HUM100 NWH 12348 XU PAULINE
MAT241 YRK 12365 JORDAN GEOFF PHY101 YRK
12378 LI TONY BIO233 YRK 12444 YIN
PAUL CHM332 YRK 12699 MATHEWS PAMELA
ENG101 YRK 23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
9WRITE Statement
- WRITE Statement
- SYNTAX
- WRITE record-name-1 FROM identifier-1
- INVALID KEY imperative-statement-1
- NOT INVALID KEY imperative-statement-2
- END-WRITE
- A new record is written to the indexed file.
- If the WRITE statement executes correctly, the
NOT INVALID KEY cause becomes true - Otherwise INVALID KEY clause is TRUE
Hypothetical Seneca College Student Data
File NWH 12340 JONES BOBBY CBL344 NWH
12341 DOUGLAS MICHAEL DBL526 NWH 12342
CARTER STEVEN DBS329 NWH 12345 FISHER
JENNY HUM100 NWH 12348 XU PAULINE
MAT241 YRK 12365 JORDAN GEOFF PHY101 YRK
12378 LI TONY BIO233 YRK 12444 YIN
PAUL CHM332 YRK 12699 MATHEWS PAMELA
ENG101 YRK 23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
10Random WRITE of a Record
- Example Write a new student file record for
Peter Ho in the George Town campus - DATA DIVISION.
- FILE SECTION.
- FD STUDENT-FILE.
- 01 STUDENT-FILE-REC.
- 05 STUDENT-KEY.
- 10 CAMPUS PIC X(03).
- 88 NEWHAM VALUE NWH.
- 88 SENECA-AT-YORK VALUE YRK.
- 88 GEORGE-TOWN VALUE GTW.
- 10 STUDENT-NUM PIC 9(05).
- 05 STUDENT-FNAM PIC X(10).
- 05 STUDENT-LNAM PIC X(10).
- 05 COURSE PIC X(06).
- PROCEDURE DIVISION.
- SET GEORGE-TOWN TO TRUE
- MOVE 12340 TO STUDENT-NUM
- MOVE HO TO STUDENT-LNAM
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
HUM100 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
11Updating an Indexed File Randomly - REWRITE
- OPEN I-O Statement
- The file must first be OPENed for both input and
output - REWRITE Statement
- SYNTAX
- REWRITE record-name-1 FROM identifier-1
- INVALID KEY imperative-statement-1
- NOT INVALID KEY imperative-statement-2
- END-REWRITE
- The record is first READ into the Main Memory of
the program and can be re-written using the
REWRITE statement. (A READ always precedes a
REWRITE.) - Old data can be replaced by new values.
- The KEY cannot be changed with a REWRITE
statement. - If sequentially reading of an indexed file is
being done, a REWRITE statement should not be
issued after the End-of-File condition becomes
true.
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
HUM100 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
12Updating an Indexed File Randomly - REWRITE
- EXAMPLE Change Jenny Fishers COURSE from
HUM100 to CBL344. - PROCEDURE DIVISION.
- OPEN I-O STUDENT-FILE
- SET NEWHAM TO TRUE
- MOVE 12345 TO STUDENT-NUM
- READ STUDENT-FILE
- INVALID KEY
- DISPLAY RECORD NOT ON FILE
- NOT INVALID KEY
- MOVE CBL344 TO COURSE
- REWRITE STUDENT-FILE-REC
- INVALID KEY DISPLAY REWRITE ERROR
- END-REWRITE
- END-READ
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
CBL344 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
13Generic Reads of an Indexed File
- START Statement
- Suppose you wanted to read only records of
students in the Newnham campus, then the START
verb can be used - SYNTAX
- START file-name-1 KEY rel-op data-name-1
- INVALID KEY imperative-statement-1
- NOT INVALID KEY imperative-statement-2
- END-START
- rel-op the relative operators (, gt, lt, gt, lt,
NOT) - START statement
- Successful execution of the START statement will
position the Current Record Pointer at the first
record that matches the condition - No data is read into the program however
- This is referred to as a Generic Read
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
HUM100 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
14Generic Reads of an Indexed File
- START Statement
- To get the data after a START statement
successfully executes, one must issue a READ
NEXT. - READ NEXT used for
- sequentially reading from a file that has been
accessed dynamically - beginning a sequential read from some point other
thn the beginning of the file - is not required when ACCESS IS SEQUENTIAL is
specified
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
HUM100 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
15Generic Reads of an Indexed File - START
- EXAMPLE Read only records of students from the
Newnhan Campus and display names. - ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- FILE-CONTROL.
- SELECT STUDENT-FILE
- ASSIGN TO DISK-SNCFLE
- ORGANIZATION IS INDEXED
- ACCESS IS DYNAMIC
- RECORD KEY IS STUDENT-KEY.
- DATA DIVISION.
- FILE SECTION.
- FD STUDENT-FILE.
- 01 STUDENT-FILE-REC.
- 05 STUDENT-KEY.
- 10 CAMPUS PIC X(03).
- 88 NEWHAM VALUE NWH.
- 88 SENECA-AT-YORK VALUE YRK.
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
HUM100 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
16Generic Reads of an Indexed File - START
- EXAMPLE (Continued) Read only records of
students from the Newnhan Campus, display names. - WORKING-STAORAGE SECTION.
- 01 WS-EOF PIC 9(01) VALUE 0.
- 01 WS-CAMPUS PIC X(03) VALUE NWH.
- PROCEDURE DIVISION.
- OPEN INPUT STUDENT-FILE
- MOVE NWH TO CAMPUS
- MOVE LOW-VALUES TO STUDENT-NUM
- START STUDENT-FILE KEY gt STUDENT-KEY
- INVALID KEY
- DISPLAY RECORD NOT ON FILE
- NOT INVALID KEY
- PERFORM R100-READ-STUDENT-FILE
- PERFORM UNTIL
- (WS-EOF 1) OR (CAMPUS NOT
WS-CAMPUS) - DISPLAY STUDENT-FNAM STUDENT-LNAM
- PERFORM R100-READ-STUDENT-FILE
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
HUM100 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name
17Deleting a Record on an Indexed File Randomly
- DELETE Statement
- The file must first be OPENed for both input and
output - And the record must first be read into memory
- SYNTAX
- DELETE indexed-file-name-1 RECORD
- INVALID KEY imperative-statement-1
- NOT INVALID KEY imperative-statement-2
- END-DELETE
- EXAMPLE Delete Jenny Fisher
- PROCEDURE DIVISION.
- OPEN I-O STUDENT-FILE
- SET NEWHAM TO TRUE
- MOVE 12345 TO STUDENT-NUM
- READ STUDENT-FILE
- INVALID KEY
- DISPLAY RECORD NOT ON FILE
Hypothetical Seneca College Student Data
File GTW 12340 HO PETER DBT544 NWH
12340 JONES BOBBY CBL344 NWH 12341
DOUGLAS MICHAEL DBL526 NWH 12342 CARTER
STEVEN DBS329 NWH 12345 FISHER JENNY
CBL344 NWH 12348 XU PAULINE MAT241 YRK
12365 JORDAN GEOFF PHY101 YRK 12378 LI
TONY BIO233 YRK 12444 YIN PAUL
CHM332 YRK 12699 MATHEWS PAMELA ENG101 YRK
23333 SMITH JOHN FRE232
Course Code
Campus Code
First Name
Student Number
Last Name