Chapter 13
Binary Input/Output
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
Computer Science: A Structured Programming Approach Using C 1
13-1 Text versus Binary Streams
In this section, we compare and contrast text streams
versus binary streams.
Topics discussed in this section:
Text and Binary Files
Differences between Text and Binary Files
State of a File
Opening Binary Files
Closing Binary Files
Computer Science: A Structured Programming Approach Using C 2
FIGURE 13-1 Reading and Writing Text Files
Computer Science: A Structured Programming Approach Using C 3
Note
Formatted input/output, character input/output, and string
input/output functions can be used only with text files.
Computer Science: A Structured Programming Approach Using C 4
FIGURE 13-2 Block Input and Output
Computer Science: A Structured Programming Approach Using C 5
FIGURE 13-3 Binary and Text Files
Computer Science: A Structured Programming Approach Using C 6
Note
Text files store data as a sequence of characters; binary files
store data as they are stored in primary memory.
Computer Science: A Structured Programming Approach Using C 7
Table 13-1 File Modes
Computer Science: A Structured Programming Approach Using C 8
FIGURE 13-4 File States
Computer Science: A Structured Programming Approach Using C 9
FIGURE 13-5 File-Opening Modes
Computer Science: A Structured Programming Approach Using C 10
13-2 Standard Library Functions
for Files
C has eight categories of standard file library
functions. We have already discussed the first four in
Chapter 7 and Chapter 11. We discuss the other four
categories, which are more related to binary files, in
this section.
Topics discussed in this section:
Block Input/Output Functions
File Status FunctionsComments
Positioning Functions
System File Operations
Computer Science: A Structured Programming Approach Using C 11
FIGURE 13-6 Types of Standard Input/Output Functions
Computer Science: A Structured Programming Approach Using C 12
FIGURE 13-7 File Read Operation
Computer Science: A Structured Programming Approach Using C 13
PROGRAM 13-1 Read File of Integers
Computer Science: A Structured Programming Approach Using C 14
FIGURE 13-8 Reading a Structure
Computer Science: A Structured Programming Approach Using C 15
PROGRAM 13-2 Read Student File
Computer Science: A Structured Programming Approach Using C 16
FIGURE 13-9 File Write Operation
Computer Science: A Structured Programming Approach Using C 17
FIGURE 13-10 Writing a Structure
Computer Science: A Structured Programming Approach Using C 18
PROGRAM 13-3 Write Structured Data
Computer Science: A Structured Programming Approach Using C 19
FIGURE 13-11 Rewind File
Computer Science: A Structured Programming Approach Using C 20
FIGURE 13-12 Current Location (ftell) Operation
Computer Science: A Structured Programming Approach Using C 21
FIGURE 13-13 File Seek Operation
Computer Science: A Structured Programming Approach Using C 22
PROGRAM 13-4 Append Two Binary Files
Computer Science: A Structured Programming Approach Using C 23
PROGRAM 13-4 Append Two Binary Files
Computer Science: A Structured Programming Approach Using C 24
PROGRAM 13-4 Append Two Binary Files
Computer Science: A Structured Programming Approach Using C 25