29
©Brooks/Cole, 2003 Chapter 13 File Structures

©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

Embed Size (px)

Citation preview

Page 1: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Chapter 13

FileStructures

Page 2: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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.

Page 3: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

ACCESSACCESSMETHODSMETHODS

ACCESSACCESSMETHODSMETHODS

13.113.1

Page 4: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-1

Taxonomy (分類 ) of file structures

Page 5: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

SEQUENTIALSEQUENTIALFILESFILES

SEQUENTIALSEQUENTIALFILESFILES

13.213.2

Page 6: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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.

Page 7: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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.

Page 8: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of 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

Page 9: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-3

Updating a sequential file--process

Page 10: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-4

Updating process

Page 11: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

INDEXEDINDEXEDFILESFILES

INDEXEDINDEXEDFILESFILES

13.313.3

Page 12: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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

Page 13: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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.

Page 14: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-6

Logical view of an indexed file

Page 15: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

HASHEDHASHEDFILESFILES

HASHEDHASHEDFILESFILES

13.413.4

Page 16: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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).

Page 17: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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

Page 18: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-8

Direct hashing method

Page 19: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-9

Modulo division method

Page 20: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Digit extraction method

• For example: 125870 158 122801 128 121267 112 … 123413 134

Page 21: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-10

Collision

• The set of keys that hash to the same address in our list are called synonyms ( 同名 ).

Page 22: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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

Page 23: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-11

Open addressing resolution

Page 24: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-12

Linked list resolution

Page 25: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-13

Bucket (桶狀物 ) hashing resolution

• A bucket is a node that can accommodate ( 容納 ) more than one record.

Page 26: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

TEXTTEXTVERSUSVERSUSBINARYBINARY

TEXTTEXTVERSUSVERSUSBINARYBINARY

13.513.5

Page 27: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©Brooks/Cole,

2003

Figure 13-14

Text and binary interpretations of a file

Page 28: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential 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.

Page 29: ©Brooks/Cole, 2003 Chapter 13 File Structures. ©Brooks/Cole, 2003 Understand the file access methods. Describe the characteristics of a sequential file

©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