Chapter 9. File and Stream
Chapter 9. File and Stream
September 2015
Paris Saclay
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
Introduction
Paris Saclay
Introduction
Paris Saclay
Introduction
Paris Saclay
Advantages of Streams
Paris Saclay
Advantages of Streams
Paris Saclay
Advantages of Streams
Paris Saclay
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
The istream and ostream classes are derived from ios and
are dedicated to input and output
Paris Saclay
10
The istream and ostream classes are derived from ios and
are dedicated to input and output
Paris Saclay
11
The istream and ostream classes are derived from ios and
are dedicated to input and output
Paris Saclay
12
The istream and ostream classes are derived from ios and
are dedicated to input and output
Paris Saclay
13
The istream and ostream classes are derived from ios and
are dedicated to input and output
Paris Saclay
14
Introduction
Paris Saclay
15
Formatting Flags
Formatting flags are a set of enum definitions in ios.
Paris Saclay
16
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
17
Functions
get()
get()
getline()
Paris Saclay
18
Functions
Paris Saclay
19
What is it
Paris Saclay
20
Classes
How to save data to disk files and read it back in? Working with
disk files requires a set of classes
I
Paris Saclay
21
Paris Saclay
22
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
23
Paris Saclay
24
Paris Saclay
25