41
1 Objectives To understand the differences between text and binary files To write programs that read, write, and/or append binary files To be able to detect and take appropriate action when file errors occur To be able to process files randomly To be able to create text files from binary files and vice versa To understand and be able to implement file merges To understand and be able to implement the classic sequential file update Chapter 13 Chapter 13 Binary Input/Output Binary Input/Output

1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

Embed Size (px)

Citation preview

Page 1: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

1

Objectives ❏ To understand the differences between text and binary files❏ To write programs that read, write, and/or append binary files❏ To be able to detect and take appropriate action when file errors occur❏ To be able to process files randomly❏ To be able to create text files from binary files and vice versa❏ To understand and be able to implement file merges❏ To understand and be able to implement the classic sequential file update

Chapter 13Chapter 13 Binary Input/OutputBinary Input/Output

Page 2: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

2

13-1 Text versus Binary Streams

In this section, we compare and contrast text streams In this section, we compare and contrast text streams versus binary streams. versus binary streams.

Text and Binary FilesDifferences between Text and Binary FilesState of a FileOpening Binary FilesClosing Binary Files

Topics discussed in this section:Topics discussed in this section:

Page 3: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

3

FIGURE 13-1 Reading and Writing Text Files

Formatted input/output, character input/output, and string input/output functions can be used only with text files.

NoteNote

Page 4: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

4

FIGURE 13-2 Block Input and Output

Page 5: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

5

FIGURE 13-3 Binary and Text Files

512+256=768文數字的 ASCII

Text files store data as a sequence of characters; binary files store data as they are stored in primary memory.

NoteNote

Page 6: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

6

Table 13-1 File Modes

Page 7: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

7

FIGURE 13-4 File States

Page 8: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

8

FIGURE 13-5 File-Opening Modes

Page 9: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

9

13-2 Standard Library Functions for Files

C has eight categories of standard file library C has eight categories of standard file library functions. We have already discussed the first four in functions. We have already discussed the first four in Chapter 7 and Chapter 11. We discuss the other four Chapter 7 and Chapter 11. We discuss the other four categories, which are more related to binary files, in categories, which are more related to binary files, in this section.this section.

Block Input/Output FunctionsFile Status FunctionsCommentsPositioning FunctionsSystem File Operations

Topics discussed in this section:Topics discussed in this section:

Page 10: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

10

FIGURE 13-6 Types of Standard Input/Output Functions

Page 11: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

11

FIGURE 13-7 File Read Operation

Page 12: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

12

PROGRAM 13-1 Read File of Integers

Page 13: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

13

FIGURE 13-8 Reading a Structure

Page 14: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

14

PROGRAM 13-2 Read Student File

Page 15: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

15

FIGURE 13-9 File Write Operation

Page 16: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

16

FIGURE 13-10 Writing a Structure

Page 17: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

17

PROGRAM 13-3 Write Structured Data

Page 18: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

18

FIGURE 13-11 Rewind File

Page 19: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

19

FIGURE 13-12 Current Location (ftell) Operation

Page 20: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

20

FIGURE 13-13 File Seek Operation

Page 21: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

21

PROGRAM 13-4

Append Two Binary Files

Page 22: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

22

13-3 Converting File Type

A rather common but somewhat trivial problem is to A rather common but somewhat trivial problem is to convert a text file to a binary file and vice versa. C has convert a text file to a binary file and vice versa. C has no standard functions for these tasks. We must write a no standard functions for these tasks. We must write a program to make the conversion. We describe the file program to make the conversion. We describe the file conversion logic in this section.conversion logic in this section.

Creating a Binary File from a Text FileCreating a Text File from a Binary File

Topics discussed in this section:Topics discussed in this section:

Page 23: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

23

FIGURE 13-14 Create Binary File Structure Chart

Page 24: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

24

PROGRAM 13-5 Text to Binary Student File

Page 25: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

25

Page 26: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

26

Page 27: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

27

Page 28: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

28

FIGURE 13-15 Design for Print Student Data

Page 29: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

29

PROGRAM 13-6

Print Student Data

Page 30: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

30

Page 31: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

31

Page 32: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

32

Page 33: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

33

Page 34: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

34

13-4 File Program Examples

This section contains two common file applications. This section contains two common file applications. The first uses the file positioning functions to The first uses the file positioning functions to randomly process the data in a file. The second merges randomly process the data in a file. The second merges two files.two files.

Random File ProcessingMerge Files

Topics discussed in this section:Topics discussed in this section:

Page 35: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

35

PROGRAM 13-7

Random File Application

Page 36: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

36

PROGRAM 13-8

Random File: Build File

Page 37: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

37

PROGRAM 13-9 Random File: Sequential Print

Page 38: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

38

PROGRAM 13-10 Random File: Random Print

Page 39: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

39

FIGURE 13-16 File Merge Concept

Page 40: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

40

ALGORITHM 13-1 Pseudocode for Merging Two Files

Page 41: 1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able

41

PROGRAM 13-11 Merge Two Files