Вы находитесь на странице: 1из 25

Chapter 9.

STREAMS AND FILES

Chapter 9. STREAMS AND FILES


Minh Quang NGUYEN
Hanoi National University of Education

September 2015

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES

Stream Classes
The ios Class
The istream Class
The ostream Class
The iostream class
Disk File I/O with Streams
You do it
Q&A
Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

Introduction

A stream is a general name given to a flow of data.

In C++ a stream is represented by an object of a particular


class.

So far weve used the cin and cout stream objects.

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

Introduction

A stream is a general name given to a flow of data.

In C++ a stream is represented by an object of a particular


class.

So far weve used the cin and cout stream objects.

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

Introduction

A stream is a general name given to a flow of data.

In C++ a stream is represented by an object of a particular


class.

So far weve used the cin and cout stream objects.

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

Advantages of Streams

They are more simpler than traditional C functions such as


printf() and scanf(), and - for files - fprintf(), fscanf().

One can overload existing operators and functions, such as


the insertion (<<) and extraction (>>) operators, to work with
classes created.

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

Advantages of Streams

They are more simpler than traditional C functions such as


printf() and scanf(), and - for files - fprintf(), fscanf().

One can overload existing operators and functions, such as


the insertion (<<) and extraction (>>) operators, to work with
classes created.

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

Advantages of Streams

They are more simpler than traditional C functions such as


printf() and scanf(), and - for files - fprintf(), fscanf().

One can overload existing operators and functions, such as


the insertion (<<) and extraction (>>) operators, to work with
classes created.

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

Advantages of Streams
In an environment with a graphical user interface such as
Windows, where direct text output to the screen is not used...

They are the best way to write data to files, and also to format
data in memory for later use in text input/output windows and
other GUI elements.

Paris Saclay

Minh Quang NGUYEN

Chapter 9. STREAMS AND FILES


Stream Classes

What have we known?

The extraction operator >> is a member of the istream class,


insertion operator << is a member of the ostream class.

The istream and ostream classes are derived from ios and
are dedicated to input and output

The istream class contains such functions as get(),


getline(), read(), and the overloaded extraction (>>)
operators

Theostream contains put() and write(), and the overloaded


insertion (<<) operators.

Paris Saclay

Minh Quang NGUYEN

10

Chapter 9. STREAMS AND FILES


Stream Classes

What have we known?

The extraction operator >> is a member of the istream class,


insertion operator << is a member of the ostream class.

The istream and ostream classes are derived from ios and
are dedicated to input and output

The istream class contains such functions as get(),


getline(), read(), and the overloaded extraction (>>)
operators

Theostream contains put() and write(), and the overloaded


insertion (<<) operators.

Paris Saclay

Minh Quang NGUYEN

11

Chapter 9. STREAMS AND FILES


Stream Classes

What have we known?

The extraction operator >> is a member of the istream class,


insertion operator << is a member of the ostream class.

The istream and ostream classes are derived from ios and
are dedicated to input and output

The istream class contains such functions as get(),


getline(), read(), and the overloaded extraction (>>)
operators

Theostream contains put() and write(), and the overloaded


insertion (<<) operators.

Paris Saclay

Minh Quang NGUYEN

12

Chapter 9. STREAMS AND FILES


Stream Classes

What have we known?

The extraction operator >> is a member of the istream class,


insertion operator << is a member of the ostream class.

The istream and ostream classes are derived from ios and
are dedicated to input and output

The istream class contains such functions as get(),


getline(), read(), and the overloaded extraction (>>)
operators

Theostream contains put() and write(), and the overloaded


insertion (<<) operators.

Paris Saclay

Minh Quang NGUYEN

13

Chapter 9. STREAMS AND FILES


Stream Classes

What have we known?

The extraction operator >> is a member of the istream class,


insertion operator << is a member of the ostream class.

The istream and ostream classes are derived from ios and
are dedicated to input and output

The istream class contains such functions as get(),


getline(), read(), and the overloaded extraction (>>)
operators

Theostream contains put() and write(), and the overloaded


insertion (<<) operators.

Paris Saclay

Minh Quang NGUYEN

14

Chapter 9. STREAMS AND FILES


The ios Class

Introduction

The ios class is the granddaddy of all the stream classes

The three most important features are the formatting flags,


the error-status flags, and the file operation mode.

Paris Saclay

Minh Quang NGUYEN

15

Chapter 9. STREAMS AND FILES


The ios Class

Formatting Flags
Formatting flags are a set of enum definitions in ios.

Paris Saclay

Minh Quang NGUYEN

16

Chapter 9. STREAMS AND FILES


The ios Class

Formatting Flags

They are must be preceded with the name ios and the
scope-resolution operator (for example, ios::skipws).
cout.setf(ios::left); // left justify output text
cout >> \This text is left-justified";
cout.unsetf(ios::left); // return to default (right justified)

Paris Saclay

Minh Quang NGUYEN

17

Chapter 9. STREAMS AND FILES


The istream Class

Functions

The istream class, which is derived from ios, performs


input-specific activities, or extraction. The functions which are
most commonly used from the istream class.
I

get()

get()

getline()

Paris Saclay

Minh Quang NGUYEN

18

Chapter 9. STREAMS AND FILES


The ostream Class

Functions

The ostream class handles output or insertion activities. The most


commonly used member functions
I

Paris Saclay

Minh Quang NGUYEN

19

Chapter 9. STREAMS AND FILES


The iostream class

What is it

It is derived from both istream and ostream, acts only as a base


class from which other classes, specifically iostream withassign,
can be derived. There are three withassign classes:
I

istream withassign, derived from istream

ostream withassign, derived from ostream

iostream withassign, derived from iostream

Paris Saclay

Minh Quang NGUYEN

20

Chapter 9. STREAMS AND FILES


Disk File I/O with Streams

Classes

How to save data to disk files and read it back in? Working with
disk files requires a set of classes
I

ifstream for input

ofstream for output

fstream for both input and output

Objects of these classes can be associated with disk files, and we


can use their member functions to read and write to the files.

Paris Saclay

Minh Quang NGUYEN

21

Chapter 9. STREAMS AND FILES


Disk File I/O with Streams

Formatted File I/O

In formatted I/O, numbers are stored on disk as a series of


characters.
Example: 6.02, rather than being stored as a 4-byte type float or an
8-byte type double, is stored as the characters 6, ., 0, and 2

Paris Saclay

Minh Quang NGUYEN

22

Chapter 9. STREAMS AND FILES


Disk File I/O with Streams

Writing Data
Writes a character, an integer, a type double, and two string
objects to a disk file.
char ch = x;
int j = 77;
double d = 6.02;
string str1 = \Kafka"; //strings without
string str2 = \Proust"; // embedded spaces
ofstream outfile(\fdata.txt"); //create ofstream object
outfile << ch //insert (write) data
<< j
<< //needs space between numbers
<< d
<< str1
<< //needs spaces between strings
<< str2;
Paris Saclay

Minh Quang NGUYEN

23

Chapter 9. STREAMS AND FILES


You do it

Paris Saclay

Minh Quang NGUYEN

24

Chapter 9. STREAMS AND FILES


Q&A

QUESTION and ANSWER

Paris Saclay

Minh Quang NGUYEN

25

Вам также может понравиться