CPP IV C Streams
CPP IV C Streams
CPP IV C Streams
Unit IV
• Streams and Files:
– Streams Classes, Stream Errors.
– Disk File I/O with Streams,
– File Pointers, Error Handling in File I/O,
– File I/O With Member Function,
– Overloading the Extraction And Insertion
Operators
1
C++ Streams
Input and Output
• What is a stream?
– Think of a stream as a sequence of characters
2
C++ Streams
The stream model
data source:
our
ourprogram
program
data destination:
3
C++ Streams
File input and output
4
C++ Streams
Why use streams? (Advantages of Streams)
• One reason is simplicity.
• you can overload existing operators and functions, such as the
insertion(<<) and extraction (>>) operators, to work with classes
that you create.
• printf potentially wastes run-time memory
– entire printf library is loaded to output a single char
• printf is theoretically slower than C++ streams
– the format string is parsed at run-time instead of compile-time
– the C library does benefit from more years of performance
tuning, however
• printf allows no compile-time type checking
– no protection against arguments not matching the format
specifier
• printf is hard to extend to new types
– %c, %d, %s, %f
5
The Stream Class Hierarchy C++ Streams
6
C++ Streams
Types of streams
• Streams for:
7
C++ Streams
Stream operators
• ostream
– output is called “insertion”
– << is the “insertion” operator
– Value is inserted into the output stream
• istream
– input is called “extraction”
– >> is the “extraction” operator
– Value is extracted from the input stream
8
C++ Streams
Well-known streams
9
C++ Streams
Examples
#include <iostream>
using std::cout; using std::cin; using std::endl;
10
C++ Streams
C++ classes preview (or review)
11
C++ Streams
The ios Class
• The ios class is the granddaddy of all the stream classes, and
contains the majority of the features to operate C++ streams.
• The three most important features are
– the formatting flags,
– the error-status flags, and
– the file operation mode.
• Formatting Flags
– set of enum definitions in ios.
– They act as on/off switches that specify choices for various
aspects of input and output format and operation.
– Since they are members of the ios class, you must usually
precede them with the name ios and the scope-resolution
operator (for example, ios::skipws).
– All the flags can be set using the setf() and unsetf() ios member
functions.
12
– Many formatting flags can be set using manipulators.
C++ Streams
ios formatting flags
13
C++ Streams
Manipulators
14
C++ Streams
ios manipulators with no arguments
15
C++ Streams
ios Manipulators with Arguments
• Example : cout<<setw(20)<<“Hello”;
16
C++ Streams
ios formatting funtions
• The ios class contains a number of functions that you can use to set the formatting
flags and perform other tasks.
• cout.width(14);
• cout.setf(ios::left);
• cout.setf(ios::left, ios::adjustfield);
17
C++ Streams
The istream model
c
“somewhere”
(1,234) istream
123 buffer
• An istream
– turns character sequences into values of various types
– gets those characters from somewhere
• E.g., console, file, main memory, another computer
18
C++ Streams
istream functions
19
C++ Streams
The ostream model
c
“somewhere”
(1,234) ostream
123 buffer
• An ostream
– turns values of various types into character sequences
– sends those characters somewhere
• E.g., console, file, main memory, another computer
20
C++ Streams
The ostream Class
21
C++ Streams
The iostream and the _withassign Classes
• iostream class:
– It is derived from both istream and ostream, acts only as a base class
for some other classes.
• _withassign classes:
– It is derived from either istream, ostream or iostream. They are much
like those they’re derived from except that they include overloaded
assignment operators so their objects can be copied.
– There are three _withassign classes:
• istream_withassign, derived from istream
• ostream_withassign, derived from ostream
• iostream_withassign, derived from iostream
• the istream, ostream, and iostream classes are made uncopyable (by
making their overloaded copy constructors and assignment operators
private), while the _withassign classes derived from them can be copied.
22
C++ Streams
Predefined Stream Objects
• cin
– an object of istream_withassign, normally used for
keyboard input
• cout
– an object of ostream_withassign, normally used for
screen display
• cerr
– an object of ostream_withassign, for error messages
• clog
– an object of ostream_withassign, for log messages
23
C++ Streams
File processing
24
C++ Streams
Overloading the Extraction And Insertion Operators
C++ is able to input and output the built-in data types using the stream extraction
operator >> and the stream insertion operator <<
The stream insertion and stream extraction operators also can be overloaded to
perform input and output for user-defined types like an object.
25
C++ Streams
Example Program
26
C++ Streams
27
C++ Streams
28
C++ Streams
29
C++ Streams
30
C++ Streams
31