You are on page 1of 44

V Anu divya.

R
V Gayathri.P
V Rajeswari.R
V Sharmila.T.S
V Visalatchi.R
V Yoga Sandhya.S
V Supports two complete I/O systems:

V I/O system inherited from C

V object-oriented I/O system defined by C++

V Console I/O and disk I/O, are actually just different


perspectives on the same mechanism.

V I/O library is supported by the header <iostream>.


V C's I/O system knows nothing about objects.

V C++ I/O system operates on user-defined objects.

V The C I/O is supported by C++ only for


compatibility.
w  
§ 
§
§ § § §

   

w  

w



§ § § §
V The C++ I/O system operates through streams.

V A › is a logical device that either produces or


consumes information.

V A stream is linked to a physical device.

V All streams behave in the same way.


½ Ôi.e) same function that writes to a file to write to the
printer or to the screen
    a  §  
§   
@ t
device
Program

Ott
device
  
  § § 
  
V A stream is a sequence of bytes.
V Its acts either as a source from which the input
device be obtained or as a destination to which the
output data can be sent.
V The source stream provides data is called as input
stream.
V The destination stream receives output is called as
output stream
V In <iostream> complicated set of class hierarchies
, that supports I/O Operations are defined .
V The I/O classes begin with a system of template
classes.
V I/O system contains a hierarchy of classes that are
used to define various streams to deal with both
the console and disk files. These classes are called
streams classes.
V ios is the base class for istream and ostream which
are base classes for iostream.
V Ios provides the basic support for formatted and
unformatted I/O operations.
V Istream provides facilities for formatted and
unformatted input.
V Ostream provides facilities for formatted output.
V The class iostream provides the facilities for
handling both input and output streams.
V Three classes:
2 istream_withassign
2 ostream_withassign
2 iostream_withassign
add assignment operator to these classes


 

   

 

V An ostream
V turns values of various types into character sequences
V sends those characters somewhere
V a  , console, file, main memory, another computer


 

§  

 

V An istream
V turns character sequences into values of various types
V gets those characters from somewhere
V a  , console, file, main memory, another computer
V e have used the cin Ô ) and coutÔ) for input and
output of data.
V The operator is overloaded in the istream and 
in ostream class.
E.g.,
int code;
cin code;
coutcode is:dzcode;
Output: 78989g
Code is 78989g
V e use getÔ) and putÔ) function to handle the
single character I/O operations.
V e use both getÔchar *) and getÔvoid) prototypes
to fetch a character including the blank space, tab
and the newline character.
E.g.,
cin.getÔc);
Insteads of cin c;
V This functions is a member of ostream calss can be
used to output a line of text character by character.
V The variable must contains a character value. e
can also use a number as an argument to putÔ)
E.g.,
cout.putÔ8);
It display the character D.
void mainÔ)
{ int count=0; result:
whileÔc!=Ǯ\nǯ) input value: oops
{ output value: oops
cout.putÔc);
count++;
cin.getÔc);
}
}
V The getlineÔ) function reads a whole line of text
that ends with a newline character.
÷ tax: cin.getlineÔline,size);
V This function call invokes which reads character
input into the variable line.
E.g., char name[20];
cin.getlineÔname,20);
V writeÔ) function does the work of cout funtion
V This function displays an entire line.
s tax: cout.writeÔline,size);
V The first argument line represents the name of the
string to be displayed and the second argument
size indicates the number of character to display.
E.g., char s1=gayathridz;
cout.writeÔs1,5);
void mainÔ)
{
char string1[10];
Cin.getlineÔstring,8);
Coutthe character is:dz;
Cout.writeÔstring,8);
} ott:
Gayathri
the character is: Gayathri
V C++ supports a number of features that could be
used for formatting the output.
V The features are:
o ios class function and flags
o Manipulators
o user defined output functions
V The ios class contains a large number of member
functions tat would help us to format the output in
a number of ways.
V The important ios functions are
idthÔ)
PrecisionÔ)
fillÔ)
SetfÔ)
UnsetfÔ)
V This widthÔ) function is used to specify the required
filed size for displaying an output value.
÷ tax: cout.widthÔw);
E.g.,
Cout.widthÔ5);
Cout54312;
— 2
2
  2  2  
  2 
2   
@  


   
 
X 
 
— 2
2
  2 
 
  
2   

@  


   
 
V The precisionÔ) function is used to specify the
number of digits to be displayed after the decimal
point of a float value.
÷ tax: cout.precisionÔd);
where d is the no.of.digits to the right of the decimal
point.
E.g., cout.precisionÔ2); ott:
cout3.141334; 3.14
— 2
2      !" #
 
2
@  

   
   
   
   
  
— 2
2      !" #
        
  2  
@  


   
   
  
  $
a   §
  § 
V This functions is used to specify a character that is
used to fill the unused portion of a field.
÷ tax: cout.fillÔch);
where ch- the character used to filling the unused
positions.
E.g., cout.fillÔǮ*ǯ); ott:
cout.widthÔ5);
cout52; * * * 5 2
§ § 
$    
  §§ %
 §% R 
&& '&&() *%
 
 §

+!+% !!!!! 
 §% !!!!!!!"§#
&& '&&() *%  !!!!!

 §%
&&("§#(&&() (%
 §%
 §,,
%
&& '%
-
V The setfÔ) functions is used to specify format flags
tat can control the form of output displayÔ left or
right justification)
÷ tax: cout.setfÔarg1,arg2);
V The arg1 is one of the formatting flags defined in
the class ios. The arg2 known as bit field specifies
the group to which the formatting flag belongs.
ormat reqired  ag (arg1) Bit-fie d (arg2)
>eft justified ios::left ios::adjustfield
Right justified ios::right ios::adjustfield
Padding after sign or base
Ios::internal ios::adjustfield
indicator
Scientific notation ios::scientific ios::floatfield
Fixed point notation ios.scientific ios::floatfield
Decimal base ios::dec ios::basefield
Octal base ios::oct ios::basefield
Hexadecimal base ios::hex ios::basefield

    §§


 ag Mea i g
Ios::showbase Use base indicator on output
Ios::showpos Print + before positive numbers
Ios::showpoint Show trailing decimal point and 0s
Ios::uppercase Use uppercase letters for hex output
Ios::skiplus Skip white space on input
Ios::unitbuf Flush all streams after insertio
Ios::stdio Flush stdout and stderr after insertion
V For real numbers, trailing zeros are discarded
when displayed
@  

   
   §§     

V To display trailing zeros we use:


2 — 2%%2
2 
— 2
2
  2 
     
  2   
2  — 2%%   && 
2 

@  


   
 
V unsetf function member of ios is used to clear the
flags
÷ tax:
void unsetfÔfmtflags — ›);
E.g.,
cout.unsetfÔios::uppercase);
int mainÔ)
{
cout.setfÔios::uppercase | ios::scientific);
cout  100.12; áá›  ›

a
cout.unsetfÔios::uppercase); áá   ›
cout  100.12; áá›  ›


return 0;
}
V Manipulators are special functions that can be
included in the I/O statements to alter the format
parameter of a stream.

Ma i ators Eqiva e t to ios f ctio


SetwÔ) idthÔ)
SetprecisionÔ) PrecisionÔ)
SetfillÔ) FillÔ)
SetiosflagsÔ) SetfÔ)
ResetiosflagsÔ) UnsetfÔ)
Ma i ators Mea i g Eqiva e t
SetwÔint w) Set the field width to w idthÔ)
SetprecisionÔint d) Set the floating point precison to d PrecisionÔ)
SetfillÔint c) Set the fill character to c FillÔ)
SetiosflagsÔlong f) Set the format flag SetfÔ)
ResetiosflagÔlong f) Clear the flag specified by f UnsetfÔ)
Endl Insert new line and flush stream \ndz
program using manipulators áá  § 

#include iostream .§ 
&§  /
#include iomanip .§ 
&§ §/
using namespace std;
§   %
int mainÔ)
§  § 
{
$
cout  hex  100  endl;
cout  setfillÔ'?')  setwÔ10)  §,, §,, §
%
2343.0; &&&&() (%á᧠
return 0;  §

+0+%
}  §%
&& %
Ott :  %
64
-
??????2343

w  
      §
V e can also design our own manipulators for certain
special purpose.
The general form:
ostream & manipulatorÔostream & output)
{
ǥǥǥǥ
ǥǥǥǥ Ôcode)
ǥǥǥǥ
return output;
}
ostream & unitÔostream & output)
{
outputinchesdz;
return output;
}

The statement :
Cout3unit; output:
3 inches

You might also like