0% found this document useful (0 votes)
20 views14 pages

Binary IO

Uploaded by

Thien Bui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Binary IO

Uploaded by

Thien Bui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

12/2/2021

MSOffice1

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

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

1
Slide 1

MSOffice1 , 9/20/2005
12/2/2021

FIGURE 13-1 Reading and Writing Text Files

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

2
12/2/2021

FIGURE 13-2 Block Input and Output

FIGURE 13-3 Binary and Text Files

3
12/2/2021

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

Table 13-1 File Modes

4
12/2/2021

FIGURE 13-4 File States

FIGURE 13-5 File-Opening Modes

10

5
12/2/2021

13-2 Standard Library Functions


for Files

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
11

FIGURE 13-6 Types of Standard Input/Output Functions

12

6
12/2/2021

FIGURE 13-7 File Read Operation

13

PROGRAM 13-1 Read File of Integers

14

7
12/2/2021

FIGURE 13-8 Reading a Structure

15

PROGRAM 13-2 Read Student File

16

8
12/2/2021

FIGURE 13-9 File Write Operation

17

FIGURE 13-10 Writing a Structure

18

9
12/2/2021

PROGRAM 13-3 Write Structured Data

19

FIGURE 13-11 Rewind File

Computer Science: A Structured Programming Approach Using C 20

10
12/2/2021

FIGURE 13-12 Current Location (ftell) Operation

21

FIGURE 13-13 File Seek Operation

22

11
12/2/2021

PROGRAM 13-4 Append Two Binary Files

23

PROGRAM 13-4 Append Two Binary Files

24

12
12/2/2021

PROGRAM 13-4 Append Two Binary Files

25

13

You might also like