![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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/40.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062409/5697c0101a28abf838ccb05b/html5/thumbnails/41.jpg)
41
PROGRAM 13-11 Merge Two Files