Stream
A stream is a general name given
to the flow of data. Different streams are used to represents different kinds
of data flow. For example, the standard output stream flows to the screen
display, the standard input stream flows from the keyboard. In C++ a stream is
represented by an object of a particular class. For example, cin and cout that
we have used so far are really objects of istream_withassign and
ostream_withassign classes respectively. These classes have been derived from
istream and ostream classes. Both these classes are including in “iostream.h”
header file.
An iostream system consists of
three parts:
a) A
buffer, which acts as an intermediary between the generalized input – output
system and some particular source or sink for characters. This has been
implemented in the streambuf classes.
b) A specifications system responsible for
reporting errors and controlling formats. This has been implemented in the ios
class.
c) A
translation system that converts C++ language’s typed objects to a sequence of
characters or vice versa. This has been implemented in classes like istream,
ostream, iostream etc.
The ios class
The ios class contains data
members that help formatting of stream of data, the error – status flags and
the file operation mode. Some of the data used for formatting is stored in
variables. For example, the floating – point precision, the output field width,
and the characters used to pad the output. The rest of the formatting is
determined by flags, which are usually combined to save space.
Some flag example are given
below:
dec
|
Convert to decimal
|
oct
|
Convert to octal
|
hex
|
Convert to hexadecimal
|
left
|
Left justify output
|
right
|
Right justify output
|
The istream Class
The istream class is derived from
the ios class. It performs activities specific to input. One of the most
commonly used member function of this is the overload ‘>>’ operator. It
has been overloaded to extract values of all basic types. We can extract even a
string using this operator:
char str[10];
cin>>str;
The ostream Class
The ostream class handles output
or insertion activates. Once again the most commonly used member function of
this class is the overloaded ‘<<’ operator function.
cout<<”Welcome”;
Two other useful functions of
this class are put() and flush(). The first one puts a character into the
stream, whereas , the second flushes the buffer contents and insert a new line.
No comments:
Post a Comment