CPP IV C Streams

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

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

• C++ uses streams for input and output

• Like C, these are not defined within the language, but in


the library

• What is a stream?
– Think of a stream as a sequence of characters

• To use streams, we must add “#include <iostream>”


• All standard library components are in the std
namespace, so appropriate directives or qualifications
must be used
– using std::cout;

2
C++ Streams
The stream model

data source:

input device device driver input library

our
ourprogram
program

data destination:

output library device driver output device

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:

– Input: istream, ifstream, istringstream

– Output: ostream, ofstream, ostringstream

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

• Global stream objects are defined in <iostream>

– cin istream object connected to standard input

– cout ostream object connected to standard output

– cerr ostream object connected to standard error

– clog ostream object connected to standard error


providing buffered output

9
C++ Streams
Examples

#include <iostream>
using std::cout; using std::cin; using std::endl;

cout << “Hello, World” << endl;


// endl is equivalent to “\n”, plus a buffer flush

int nValue = 123;


cout << “Value is” << nValue << endl;

cin >> nValue;


cout << “The new value is” << nValue << endl;

10
C++ Streams
C++ classes preview (or review)

• Streams are instances of C++ classes.


• Class instances (generally called objects) are like
structures in that they have fields (generally called
members).
• Members can be data or functions (methods).
• We use the “.” syntax to access members

infile.get(ch); // get a character


outfile.close(); // close the stream
outfile.eof(); // end of stream?
cout.precision(); // get the current precision

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

• Manipulators are formatting instructions inserted directly


into a stream.
– cout << “To each his own.” << endl;
– cout << setiosflags(ios::fixed) // use fixed decimal
point
– cout<< setiosflags(ios::showpoint) // always show
decimal point
– cout<< var;
• manipulators come in two flavors: with no argument and
with argument manipulators

14
C++ Streams
ios manipulators with no arguments

• You insert these manipulators directly into the stream.


• Example : cout << hex << var;

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

• Must include <iostream>


• Must also include <fstream>

• File – Modes of operation


• ios::app append only
• ios::ate open and seek to the end
• ios::in input
• ios::out output
• ios::trunc if file exists, overwrite it
• ios::binary open file in binary mode

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.

Here, it is important to make operator overloading function a friend of the class


because it would be called without creating an object.

25
C++ Streams
Example Program

26
C++ Streams

27
C++ Streams

28
C++ Streams

29
C++ Streams

30
C++ Streams

31

You might also like