Files  File organisation and usage

advertisement
Files
 File organisation and usage



A record is a group of logically related fields
A file is a group of logically related records
Files are used to store
Large volumes
 Volatile information


Files are stored on secondary storage
Files
 File categories



Serial files
Indexed sequential files
Relative files
Serial Files
 Text files
 Program files
 Report files
 Work files
 Transaction files
 Sequential files
 Different record formats possible
Characteristics of Serial
Files
 Records written consecutively
 More than one record format
 Stored on serial devices or DASDs (Direct
Access Storage Devices)
 Used for sorting
 Can be used in batch mode
 Sequential access quicker
Suitable Subjects for Serial
Files
 No direct access requirement
 Large volumes of information
 Record order unimportant
 Different record types
 No updating requirement
Using Sequential Files
 Preparation



Source - key to tape, key to disk, optical
scanners, voice recognisers
Verification on communication and values
required
May be sorted or merged
Using Sequential Files
 Prepare the data





Copy the file to offline backup
Copy the file locally
Check fields
Merge multiple files
Sort
Using Sequential Files
 Declare the file to the program
 To read:




Open the file for input to the program
Read the first record
Check to see if the read failed due to end-of-file
While not end-of-file



Process the record
Read the next record
Close the file
Indexed Sequential File
Characteristics
 File management system looks after
indexing
 Records are accessed directly or sequentially
 Each record has one or more key fields
 Overuse of indexing is inefficient
 Very good multi-user capabilities
Indexed Sequential File Ideal
Subjects
 Medium to large amount of data
 Data filed mainly by one key
 Regular sequential and direct access
 Possible skip sequential access
 Low volatility and growth
 Low alternate key access requirement
Using Indexed Sequential Files
 Declaring




File and device name
Organisation (indexed)
Primary key
Alternate keys
 Creation

Writes using keys
Retrieval From Indexed
Sequential Files
 Direct access:


Give key value
Read, allowing for no record, or lock
 Direct and sequential

Read next record, allowing for end of file or lock
Retrieval From Indexed
Sequential Files
 Sequential


Specify start of range and key
Loop to
Read next record, allowing for end-of-file
and lock
Check for end of range or file
Updating Indexed Sequential
Files
 Change non-primary key values




Read record directly with lock
Change non-primary key values
Rewrite record
Unlock
Updating Indexed Sequential
Files
 Changing primary key value






Read record directly with lock
Check for new record viability
Copy record details
Write new record with lock
Delete old record
Release locks
Deleting From IS Files
 Read record with lock
 VERIFY that deletion is required
 Delete record
 Release locks
Direct Access or Relative Files
 Records can be accessed directly, according
to the number of the record within the file
 Each record has a key, which is it’s relative
address from the start of the file
 This is suitable for only a small number of
records
Direct Access or Relative
Files
 All key values must be used
 Very easy to use (using relative addressing)
 Hashing functions can be used (see
algorithms course)
Using Relative Files
 File is declared using file name and storage
device
 Organisation is relative
 An independent variable holds the key
 The record format is declared
Using Relative Files
 The file is opened for input, output or
updating
 Records can be read, added, amended using
direct access
 Records can be read sequentially from a preset point in the file
 The file is closed
Using Relative Files
 Creation


Addressing method must be implemented to
position records
Insertion of records uses direct access
Using Relative Files
 Retrieval



Sequential access if no hashing
Retries may be necessary if hashing being used
Retries may be necessary if multi-user file, because of
locking
 Updates



Direct access mode
Deletion causes gaps
Record / file locking must be implemented
Relative Files
Select Expend-file assign "Expend.dat"
organization is relative
access mode is random
relative key is month-number.
Working-Storage Section.
01 month-number
pic 99.
Procedure division
Open I-O Expend-file.
Move L-month-in to month-number.
Read Expend-file
invalid key
display "month not found for " L-month-in
not invalid key
move fields to record
rewrite expend-record
invalid key
display "Error rewriting ... "
end-rewrite
end-read
close expend-file.
Download