Upload
nigel-jordan
View
226
Download
4
Embed Size (px)
Citation preview
©Brooks/Cole,
2003
Chapter 13
FileStructures
©Brooks/Cole,
2003
Understand the file access methods.Understand the file access methods.
Describe the characteristics of a sequential file.Describe the characteristics of a sequential file.
After reading this chapter, the reader should After reading this chapter, the reader should be able to:be able to:
OOBJECTIVESBJECTIVES
Describe the characteristics of an indexed file.Describe the characteristics of an indexed file.
Describe the characteristics of a hashed file.Describe the characteristics of a hashed file.
Distinguish between a text file and a binary file.Distinguish between a text file and a binary file.
©Brooks/Cole,
2003
ACCESSACCESSMETHODSMETHODS
ACCESSACCESSMETHODSMETHODS
13.113.1
©Brooks/Cole,
2003
Figure 13-1
Taxonomy (分類 ) of file structures
©Brooks/Cole,
2003
SEQUENTIALSEQUENTIALFILESFILES
SEQUENTIALSEQUENTIALFILESFILES
13.213.2
©Brooks/Cole,
2003
Figure 13-2
Sequential file
• A sequential file is one in which records can only be accessed sequentially, one after another, from beginning to end.
©Brooks/Cole,
2003
While Not EOF{
Read the next recordProcess the record
}
Program 13.1Program 13.1 Processing records in a sequential fileProcessing records in a sequential file
• The following code show how to access all records in a sequential file.
Updating sequential files• Files involved in updating:
• New master file• The file contains the most current data
• Old master file• The permanent file that should be updates
• Transaction file• It contains changes to be applied to the
master file
• Error report file• The error report contains a listing of all errors
discovered during the update process and is presented to the user for corrective action
©Brooks/Cole,
2003
Figure 13-3
Updating a sequential file--process
©Brooks/Cole,
2003
Figure 13-4
Updating process
©Brooks/Cole,
2003
INDEXEDINDEXEDFILESFILES
INDEXEDINDEXEDFILESFILES
13.313.3
©Brooks/Cole,
2003
Indexed files• An indexed file is made of a data
file, which is a sequential file, and an index.
Mapping in an indexed file
Figure 13-5
©Brooks/Cole,
2003
Index files
• To access a record in the file:• The entire index file is loaded into
main memory.• The entries are search to find the
desired key.• The address of the record is retrieved.• Using the address, the data record is
retrieved and passed to the user.
©Brooks/Cole,
2003
Figure 13-6
Logical view of an indexed file
©Brooks/Cole,
2003
HASHEDHASHEDFILESFILES
HASHEDHASHEDFILESFILES
13.413.4
©Brooks/Cole,
2003
Figure 13-7
Mapping in a hashed file
Hashed files
• In an indexed file, the index maps the key to the address.
• An hashed file uses a function to accomplish this mapping.
• The hashed file eliminates the need for an extra file (index).
©Brooks/Cole,
2003
Hashing method• For key-address mapping, you can
select one of several hashing methods:• Direct method (Fig. 13.8)
• key=address
• Modulo division method (Fig. 13.9)• address = key % list_size +1
• Digit extraction method• Other methods
©Brooks/Cole,
2003
Figure 13-8
Direct hashing method
©Brooks/Cole,
2003
Figure 13-9
Modulo division method
©Brooks/Cole,
2003
Digit extraction method
• For example: 125870 158 122801 128 121267 112 … 123413 134
©Brooks/Cole,
2003
Figure 13-10
Collision
• The set of keys that hash to the same address in our list are called synonyms ( 同名 ).
©Brooks/Cole,
2003
Collision
• A collision is the event that occurs when a hashing algorithm produces an address for an insertion key, and that address is already occupied.
• Collision resolution ( 解答 )• Open addressing• Linked list resolution• Bucket hashing• Combination approaches
©Brooks/Cole,
2003
Figure 13-11
Open addressing resolution
©Brooks/Cole,
2003
Figure 13-12
Linked list resolution
©Brooks/Cole,
2003
Figure 13-13
Bucket (桶狀物 ) hashing resolution
• A bucket is a node that can accommodate ( 容納 ) more than one record.
©Brooks/Cole,
2003
TEXTTEXTVERSUSVERSUSBINARYBINARY
TEXTTEXTVERSUSVERSUSBINARYBINARY
13.513.5
©Brooks/Cole,
2003
Figure 13-14
Text and binary interpretations of a file
©Brooks/Cole,
2003
Text versus binary
• A text file is a file of characters.• It can not contain integers, floating-
point numbers, or any other data structures in their internal memory format.
• Example: the data sent to the printer• A binary file is a collection of data
stored in the internal format of computer.
©Brooks/Cole,
2003
Key terms• Access method• Auxiliary storage device• Binary file• Bucket hashing• Collision• Collision resolution• Data file• Digit extraction hashing• Direct hashing• Disk• Division remainder
hashing• Error report file• Hashed file• Index
• Indexed file• Key• Linked list• Linked list resolution• Modulo division• New master file• Old master file• Open addressing
resolution• Random access• Secondary storage
device• Sequential access• Sequential file• Storage device• Synonym• Tape • Text file• Transaction file