--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Mobile No : 7600044051
Email : rupareleducation@gmail.com
Chapter 9
Que. What is stream?
Ans. Generally, every program must
go through the process of input process and output. Where program take some
data as a input and generate the process data as output. For that there is a
need of technique which supply the input data to program and represent the
process data in their desire output. C++ use the concept of stream to control
the data for reading and writing. It provide string and stream class to perform
input output operation an console and disk file.
Que. What is C++ stream?
Ans. Stream
is a logical device that either produce or consume info. A stream is link to
physical device by the input output system. In C++ input output system design
to operate on wide verity of device including verity of device including
terminal, disk, drive etc…
Here, all stream behave
in the same way. The stream is an object follow by transferring data one place
to another.
Streams are classified into 1. Input Stream 2. Output Stream
In a C++ stream is use to refer to
the flow of data from particular device to the program variable.
A stream is a series of bytes. It
act either as a source from which input data can be extracted or as a destination
to which the output data can be sent. The source stream provide data to the
program call the input stream. And the destination stream that receive data
from the program call output stream.
According to above diagram
The
data in the input stream can come from the keyboard or any other storage device
while the data in the output stream can go to the screen printer or any other
storage device.
Que. C++ Stream Class.
Ans. C++
provide the concept of object oriented programming paradigm and it support
input output system of hierarchy of data manipulation. This classes are call as
stream class.
The
IOS is a the base class for istream and ostream. Which are in term base class
for iostream. The IOS is declare as the virtual base class so that only one
copy of its members are inherited in IOS stream. The class istream, ostream and
iostream which design exclusive to manage the console device. Which are declare
in the header file of iostream.h
Que. Unformatted and Formatted input output
operation.
Ans. Unformatted:
The most commonly used
object in C++ program are in cin and cout. The object cin and cout are
predefine in their header file iostream.h. which support input and output of
data of different types and this is achieved by overloading operators >>
and <<. The input expression or >> operator is overload in istream
class. While output insertion or << operator is overloaded in ostream
class.
Following are functions of
unformatted input output operation.
1.
put()
2.
get()
3.
getline()
4.
write()
1. put() : This function is
member function of ostream class. It is used to output a line of text character by character.
Syntax
: void put(char *)
According to above syntax it display a value of char type variable. It is
also possible to use no. as an
arguments. It wll consider the ascii code of specific argument given with put()
function. It
can be access
by it’s object.
2. get() : This function is the member function of input
stream class. It is used to read a single character from the input device. It has
different version of syntax.
Syntax
: void get(char *)
According
to above syntax get(char *) it assign the input char to its argument. Which
get(void)
used to return the input character. both the syntax can fetch which space character
including the
blank space, tab and newline character. It can be invoke by their object
using(.) operator.
Unformatted
IO Operation :
The
C++ stream class provide a way to read and display a line of text officially
using getline() function and write() function. getline() function normally to
input a line of text or any data that use stream object cin but the limitation
of cin is that it can not read a string after white space. It means I till
truncate a part after the white space. To prevent such a problem getline()
function is use to read whole line of text end with newline or until the
maximum limit is reached.
Write() : The write function is used to display an
entire line on the screen. The write function can be invoke aby using the cout
object. It display does not stop even when the NULL character is encounter.
Formatted
console Input Output Operation :
The
C++ input-output system allow formatted IO operation like, set a field width,
specified a number base or determine how many digit after the decimal point
will be display. C++ support a wide verity of features to perform input or
output in different terms. Formatted input output operation can be achieve
using IOS stream class member function and flag, using standard manipulator or
using user define manipulator.
Following are
some IOS class functions and flags :
1.
width()
2.
precision()
3.
fill()
4.
setf()
5.
unset()
width() : when a value is
input it occupies only as much space as the number of character it take to display it. The width() function which is a
member function of IOS class. is used to define the field to be use at time to
display the output.
precision() : when output in a floating point value than the
number of digit to be display after all decimal point can be specified use
using it by default int specify six(6) digit after the decimal point. The
function precision() is the member function of IOS class and it is use to
specify the number of digit to be display after the decimal point.
fill() : The fill()
function is the member function of IOS class and it is use to specify the
character to be display in unused portion of the display width.
setf() : The function width() is use to specify the format to
print data. C++ provide a mechanism to set the printing result in left
justified right justified scientific notation etc… the member function of IOS
class setf() is use to set flags an bit fields that control the output.
Following is
a table which show the flag value and bit field.
Flag value
|
Bit field
|
Description
|
Ios::left
|
Ios::addfield
|
Left justified output
|
Ios::right
|
Ios::add justified
|
Right justified output
|
Ios::internal
|
Ios::add justified
|
Padding between the sign or
base indigation and the number.
|
Ios::dec
|
Ios::basefield
|
Decimal conversion
|
Ios:oct
|
Ios::basefield
|
Octal conversion.
|
Ios::hex
|
Ios::basefield
|
Hexadecimal convertion
|
Ios::scientific
|
Ios::floatfield
|
Use ecponential floating
notation.
|
Ios::fixed
|
Ios::floatfield
|
Use ordinary floating notation.
|
Some of the flag
they do not have bit field are listed below.
Flag value
|
Description
|
Ios::showbase
|
Use base indigation or output
|
Ios::showpos
|
Display ‘+’ to positive number
|
Ios::showpoint
|
Include decinal point with zero
in output
|
Ios::uppercase
|
Specify uppercase for
hexadecimal output
|
unsetf() : The flag can be reset by using ios::unsetf() member
function. this function can be invoke by the cout object.
Manipulators:
The another way to alter the format of the parameter of string using a special
function call manipulators. That can be included in an input output expression.
The manipulators are function that can be use with << left shift operator
and >> right shift operator to alter the behaviour of any stream class.
all the predefine manipulators are define in the header file of iomanip.h. They
provide a same features as the ios member function and flag. Manipulators are
more convenient to use there can be more than one manipulators in a statement
and used as a chain in one statement. The manipulators are categorized in
following two category:
1.
non parameterize manipulator.
2.
parameterize manipulator.
Following are
a list of some non-parameterize manipulators:
Dec: set the conversion base to 10.
Hex: set the conversion base to 16.
Oct: set the conversion base to 8.
Endl: output in a new line.
Ends: output with a NULL character.
Flush: clear the buffer stream.
Following are
a list of some parameterize manipulators:
Setw(int width): set the field width.
Setprecision(int p): set the floating point precision.
setfill(int char): set the fill character.
setfill(int char): set the fill character.
Setbase(int base): set the conversion base 8,10 or 16.