SlideShare a Scribd company logo
Learners Support Publications
www.lsp4you.com
Managing Console I/O
Operations
1By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Managing Console I/O
Operations
• C++ uses the concept of stream and
stream classes to implement its I/O
operations with the console and disk
files.
• C++ support all of C’s rich set of I/O
functions.
2By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
C ++ Stream
• Stream is an interface supplied by the I/O
system of C++ between the programmer
and the actual device being accessed.
• It will work with devices like terminals,
disks and tape drives.
• A stream is a sequence of bytes.
• It acts either as a source from which the
input data can be obtained or as a
destination to which the output data can
be sent.
3By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
C ++ Stream
• Input Stream - The source stream that
provides data to the program.
• Output Stream - The destination stream
that receives output from the program.
continue…
Input device
Output device
Program
Input Stream
Output Stream
Extraction from
input stream
Insertion into
output stream
4By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
C ++ Stream
• The data in the input stream can come
from keyboard or any other storage
device.
• The data in the output stream can go to
the screen or any other storage device.
continue…
Input device
Output device
Program
Input Stream
Output Stream
Extraction from
input stream
Insertion into
output stream
5By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
C ++ Stream Classes
• The C++ 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 stream classes.
• These classes are declared in the header
file iostream.
6By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
C ++ Stream Classes
ios
istream ostreamstreambuf
iostream
istream_withassign iostream_withassign ostream_withassign
continue…
7By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
C ++ Stream Classes
ios
istream ostreamstreambuf
iostream
istream_withassign iostream_withassign ostream_withassign
continue…
Provides the basic
support for formatted
and unformatted I/O
operations.
Provides the facilities
for formatted and
unformatted input
Provides the
facilities for
formatted output
Provides the facilities
for handling both input
and output streams.
8By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Unformatted I/O Operations
Overloaded Operators >> and <<
• The objects cin and cout are used for
input and output of data of various types.
• By overloading the operators >> and <<.
• >> operator is overloaded in the istream
class.
• << operator is overloaded in the ostream
class.
• This is used for input data through
keyboard.
9By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Unformatted I/O Operations
Overloaded Operators >> and <<
cin >> variable1 >> varibale2 ... >> variableN
where variable1, variable2, …, variableN are valid
C++ variable names.
cout << item1 << item2 << … << itemN
Where item1, item2, …,itemN may be variables or
constants of an basic type.
10By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Unformatted I/O Operations
put( ) and get( ) Functions
• get( ) and put( ) are member functions of
istream and ostream classes.
• For single character input/output
operations.
• There are two types of get( ) functions:
– get(char*)  Assigns the input character to its argument.
– get(void)  Returns the input character.
• char c; cin.get(c) c = cin.get( );
– put( )  used to output a line of text,
character by character.
• char c; cout.put(‘x’); cout.put(c);
11By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Unformatted I/O Operations
getline( ) and write( ) Functions
• getline( ) function reads a whole line of
text that ends with a newline character.
• cin.getline(line, size);
• Reading is terminated as soon as either
the newline character ‘n’ is encountered
or size-1 characters are read.
• write( ) function displays an entire line of
text.
• cout.write(line, size);
• write( ) also used to concatenate strings.
12By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Formatted Console I/O Operations
C++ supports a number of features that
could be used for formatting the output.
These features include:
– ios class functions and flags.
– Manipulators.
– User-defined output functions.
13By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
ios member functions
width( )  to specify the required field size for
displaying an output value.
precision( )  to specify the number of digits to
be displayed after the decimal point of a float
value.
fill( )  to specify a character that is used to fill
the unused portion of a field.
setf( )  to specify format flags that can control
the form of output display.
unsetf( )  to clear the flags specified.
14By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Manipulators
Manipulators are special functions that can
be included in the I/O statement to alter the
format parameters of a stream.
To access manipulators, the file iomanip.h
should be included in the program.
– setw( )
– setprecision( )
– setfill( )
– setiosflags( )
– resetiosflags( )
15By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
width( )  Defining Field Width
To define the width of a field necessary for
the output of an item.
Since it is a member function, we have to
use an object to invoke it.
cout.width(w)
Where w is the field width (number of
columns).
The output will be printed in a field of w
characters wide at the right end of the field.
16By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
width( )  Defining Field Width
The width( ) function can specify the field
width for only one item – item that follows
immediately.
cout.width(5);
cout << 543 << 12 <<“n”;
cout.width(5);
cout << 543;
cout.width(5);
cout << 12 << “n”;
5 4 3 1 2
5 4 3 1 2
The field should be specified for
each item separately.
17By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
precision( )  Setting Precision
• Used to specify the number of digits to be
displayed after the decimal point while printing
the floating-point numbers.
• By default, the floating numbers are printed
with six digits after the decimal point.
• cout.precision(d);
– Where d is the number of digits to the right of the
decimal point.
cout.precision(3);
cout << sqrt(2) << endl;
cout << 3.14159 << endl;
cout <<2.50032 << endl;
1.141  truncated
3.142  rounded
2.5 o trailing zeros
18By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
precision( )  Setting Precision
• Unlike width( ), precision ( ) retains the setting
in effect until it is reset.
• We can also combine the field specification
with the precision setting.
cout.precision(2);
cout.width(5);
cout << 1.2345;
1 . 2 3
19By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
fill( )  Filling and Padding
• When printing values with larger field width
than required by the values, the unused
positions of the field are filled with white
spaces, by default.
• fill( ) function can be used to fill the unused
positions by any desired character.
cout.fill(ch);
where ch represents the character which is
used for filling the unused positions.
cout.fill(‘ * ’);
cout.wdith(10);
cout << 5250 << endl;
* * * * * 5 05 2*
Like precision( ), fill( ) stays
in effect till we change it.
20By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
setf( )  Formatting Flags, Bit-fields
• When the function width( ) is used, the value
will be printed right-justified in the created
width.
• setf( )function is used to print values in left-
justified.
• cout.setf(arg1, arg2)
eg:
cout.fill(‘ * ’);
cout.setf(ios :: left, ios :: adjustfield);
cout.wdith(10);
cout << “TABLE 1” << endl;
T A B L E * *1 *
arg1 is formatting flags
defined in the class ios and
arg2 is bit field constant in
the ios class.
21By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
setf( )  Formatting Flags, Bit-fields
• When the function width( ) is used, the value
will be printed right-justified in the created
width.
• setf( )function is used to print values in left-
justified.
• cout.setf(arg1, arg2)
eg:
cout.fill(‘ * ’);
cout.setf(ios :: left, ios :: adjustfield);
cout.wdith(10);
cout << “TABLE 1” << endl;
T A B L E * *1 *
arg1 is formatting flags
defined in the class ios and
arg2 is bit field constant in
the ios class.
The formatting flag
specifies the format action
required for the output.
Bit field specifies the group
to which the formatting flag
belongs.
22By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Managing Output with Manipulators
• The header file iomanip provides a set of
functions called manipulators which can be
used to manipulate the output formats.
• Some manipulators are more convenient to use
than the member functions and flags of ios.
• Two or more manipulators can be used as a
chain in one statement.
cout << manip1 << manip2 << maip3 << item;
cout << manip1 << item1 << manip2 << item2;
23By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Managing Output with Manipulators
• Manipulators and their meanings
cout << setw(5) << setprecision(2) << 1.2345
<< setw(10) << setprecision(4) << sqrt(2);
• We can jointly use the manipulators and the ios
functions in a program.
Manipulators Meaning Equivalent
setw(int w) Set the field width to w width( )
setprecision(int d) Set floating point precision to d precision( )
setfill( int c) Set the fill character to c fill( )
24By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Manipulators & ios Member Functions
• The ios member function return the previous
format state which can be used later.
• But the manipulator does not return the
previous format state.
cout.precision(2); // previous state.
int p =cout.precision(4); // current state, p=2.
cout.precision(p); // change to previous state
25By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Designing Our Own Manipulators
• We can design our own manipulators for
certain special purposes.
ostream & manipulator ( ostream & output)
{
……… (code)
return output;
}
ostream & unit (ostream & output)
{
output << “inches”;
return output;
}
cout << 36 << unit;  will produce “36 inches”.
26By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Designing Our Own Manipulators
We can also create manipulators that could
represent a sequence of operations:
ostream & show (ostream & output)
{
output.setf(ios::showpoint);
output.setf(ios::showpos);
output << setw(10);
return output;
}
This function defines a manipulator called show that
turns on the flags showpoint and showpos declared in
the class ios and sets the field width to 10.
27By:-Gourav Kottawar
Learners Support Publications
www.lsp4you.com
Thank You
28By:-Gourav Kottawar

More Related Content

What's hot (20)

PPTX
array of object pointer in c++
Arpita Patel
 
PPTX
constructors in java ppt
kunal kishore
 
PPTX
Command line arguments
Ashok Raj
 
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
PPT
Class and object in C++
rprajat007
 
PPT
Java static keyword
Lovely Professional University
 
PPTX
Operators in java
AbhishekMondal42
 
PPTX
Constructor and destructor
Shubham Vishwambhar
 
PPTX
Inheritance in OOPS
Ronak Chhajed
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPTX
Data Type Conversion in C++
Danial Mirza
 
PPTX
Stream classes in C++
Shyam Gupta
 
PPT
08 c++ Operator Overloading.ppt
Tareq Hasan
 
PPTX
Functions in python slide share
Devashish Kumar
 
PPTX
Python Functions
Mohammed Sikander
 
PPTX
Function overloading
Selvin Josy Bai Somu
 
PPT
Control structure C++
Anil Kumar
 
PPTX
Inline function
Tech_MX
 
PPTX
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
array of object pointer in c++
Arpita Patel
 
constructors in java ppt
kunal kishore
 
Command line arguments
Ashok Raj
 
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Class and object in C++
rprajat007
 
Java static keyword
Lovely Professional University
 
Operators in java
AbhishekMondal42
 
Constructor and destructor
Shubham Vishwambhar
 
Inheritance in OOPS
Ronak Chhajed
 
classes and objects in C++
HalaiHansaika
 
FUNCTIONS IN c++ PPT
03062679929
 
Data Type Conversion in C++
Danial Mirza
 
Stream classes in C++
Shyam Gupta
 
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Functions in python slide share
Devashish Kumar
 
Python Functions
Mohammed Sikander
 
Function overloading
Selvin Josy Bai Somu
 
Control structure C++
Anil Kumar
 
Inline function
Tech_MX
 
Inter Thread Communicationn.pptx
SelvakumarNSNS
 

Viewers also liked (20)

PPTX
Managing console
Shiva Saxena
 
PPT
Formatted input and output
Online
 
PDF
file handling c++
Guddu Spy
 
PPT
File handling in C++
Hitesh Kumar
 
PDF
C++ Files and Streams
Ahmed Farag
 
PPT
cpp input & output system basics
gourav kottawar
 
PDF
Programming in c++
Baljit Saini
 
PPT
Standard Template Library
Kumar Gaurav
 
PPTX
C++ io manipulation
Pedro Hugo Valencia Morales
 
PPT
Lecture 12: Classes and Files
Dr. Md. Shohel Sayeed
 
PPTX
File Handling and Command Line Arguments in C
Mahendra Yadav
 
PPT
Lec 30.31 - inheritance
Princess Sam
 
PPT
9781285852744 ppt ch10
Terry Yoast
 
PPT
9781285852744 ppt ch11
Terry Yoast
 
PPT
Templates exception handling
sanya6900
 
PPT
Lec 47.48 - stream-files
Princess Sam
 
PPTX
Control statements in c
Sathish Narayanan
 
PPTX
Data file handling in c++
Vineeta Garg
 
PPTX
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
Managing console
Shiva Saxena
 
Formatted input and output
Online
 
file handling c++
Guddu Spy
 
File handling in C++
Hitesh Kumar
 
C++ Files and Streams
Ahmed Farag
 
cpp input & output system basics
gourav kottawar
 
Programming in c++
Baljit Saini
 
Standard Template Library
Kumar Gaurav
 
C++ io manipulation
Pedro Hugo Valencia Morales
 
Lecture 12: Classes and Files
Dr. Md. Shohel Sayeed
 
File Handling and Command Line Arguments in C
Mahendra Yadav
 
Lec 30.31 - inheritance
Princess Sam
 
9781285852744 ppt ch10
Terry Yoast
 
9781285852744 ppt ch11
Terry Yoast
 
Templates exception handling
sanya6900
 
Lec 47.48 - stream-files
Princess Sam
 
Control statements in c
Sathish Narayanan
 
Data file handling in c++
Vineeta Garg
 
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
Ad

Similar to Managing console input and output (20)

PPTX
programming language in c&c++
Haripritha
 
PPTX
Managing console input
rajshreemuthiah
 
PDF
Managing I/O in c++
Pranali Chaudhari
 
PPTX
FILE OPERATIONS.pptx
DeepasCSE
 
PPT
C++InputOutput.PPT
KamranAli649587
 
PPT
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
BayanbekAmantay
 
PPTX
C++InputOutput.pptx
ansariparveen06
 
PPTX
embedded C.pptx
mohammedahmed539376
 
PDF
C programming part2
Keroles karam khalil
 
PDF
C programming part2
Keroles karam khalil
 
PDF
C programming part2
Keroles karam khalil
 
PPTX
Onnc intro
Luba Tang
 
PPTX
Lecture1_introduction to python.pptx
MohammedAlYemeni1
 
PPTX
C
Jerin John
 
PPTX
Manipulators
VaishnaviVaishnavi17
 
PDF
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
 
DOCX
Theory1&amp;2
Dr.M.Karthika parthasarathy
 
PDF
CP MATERIAL.pdf Computer Programming Material
smceramu
 
programming language in c&c++
Haripritha
 
Managing console input
rajshreemuthiah
 
Managing I/O in c++
Pranali Chaudhari
 
FILE OPERATIONS.pptx
DeepasCSE
 
C++InputOutput.PPT
KamranAli649587
 
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
BayanbekAmantay
 
C++InputOutput.pptx
ansariparveen06
 
embedded C.pptx
mohammedahmed539376
 
C programming part2
Keroles karam khalil
 
C programming part2
Keroles karam khalil
 
C programming part2
Keroles karam khalil
 
Onnc intro
Luba Tang
 
Lecture1_introduction to python.pptx
MohammedAlYemeni1
 
Manipulators
VaishnaviVaishnavi17
 
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
 
CP MATERIAL.pdf Computer Programming Material
smceramu
 
Ad

More from gourav kottawar (20)

PPTX
operator overloading & type conversion in cpp
gourav kottawar
 
PPTX
constructor & destructor in cpp
gourav kottawar
 
PPTX
classes & objects in cpp
gourav kottawar
 
PPTX
expression in cpp
gourav kottawar
 
PPTX
basics of c++
gourav kottawar
 
PPT
working file handling in cpp overview
gourav kottawar
 
PPT
pointers, virtual functions and polymorphisms in c++ || in cpp
gourav kottawar
 
PPTX
exception handling in cpp
gourav kottawar
 
PPTX
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
PPTX
constructor & destructor in cpp
gourav kottawar
 
PPTX
basics of c++
gourav kottawar
 
PPTX
classes & objects in cpp overview
gourav kottawar
 
PPTX
expression in cpp
gourav kottawar
 
PPT
SQL || overview and detailed information about Sql
gourav kottawar
 
PPT
SQL querys in detail || Sql query slides
gourav kottawar
 
PPT
Rrelational algebra in dbms overview
gourav kottawar
 
PPT
overview of database concept
gourav kottawar
 
PPT
Relational Model in dbms & sql database
gourav kottawar
 
PPTX
DBMS information in detail || Dbms (lab) ppt
gourav kottawar
 
PPTX
security and privacy in dbms and in sql database
gourav kottawar
 
operator overloading & type conversion in cpp
gourav kottawar
 
constructor & destructor in cpp
gourav kottawar
 
classes & objects in cpp
gourav kottawar
 
expression in cpp
gourav kottawar
 
basics of c++
gourav kottawar
 
working file handling in cpp overview
gourav kottawar
 
pointers, virtual functions and polymorphisms in c++ || in cpp
gourav kottawar
 
exception handling in cpp
gourav kottawar
 
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
constructor & destructor in cpp
gourav kottawar
 
basics of c++
gourav kottawar
 
classes & objects in cpp overview
gourav kottawar
 
expression in cpp
gourav kottawar
 
SQL || overview and detailed information about Sql
gourav kottawar
 
SQL querys in detail || Sql query slides
gourav kottawar
 
Rrelational algebra in dbms overview
gourav kottawar
 
overview of database concept
gourav kottawar
 
Relational Model in dbms & sql database
gourav kottawar
 
DBMS information in detail || Dbms (lab) ppt
gourav kottawar
 
security and privacy in dbms and in sql database
gourav kottawar
 

Recently uploaded (20)

PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 

Managing console input and output

  • 1. Learners Support Publications www.lsp4you.com Managing Console I/O Operations 1By:-Gourav Kottawar
  • 2. Learners Support Publications www.lsp4you.com Managing Console I/O Operations • C++ uses the concept of stream and stream classes to implement its I/O operations with the console and disk files. • C++ support all of C’s rich set of I/O functions. 2By:-Gourav Kottawar
  • 3. Learners Support Publications www.lsp4you.com C ++ Stream • Stream is an interface supplied by the I/O system of C++ between the programmer and the actual device being accessed. • It will work with devices like terminals, disks and tape drives. • A stream is a sequence of bytes. • It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent. 3By:-Gourav Kottawar
  • 4. Learners Support Publications www.lsp4you.com C ++ Stream • Input Stream - The source stream that provides data to the program. • Output Stream - The destination stream that receives output from the program. continue… Input device Output device Program Input Stream Output Stream Extraction from input stream Insertion into output stream 4By:-Gourav Kottawar
  • 5. Learners Support Publications www.lsp4you.com C ++ Stream • The data in the input stream can come from keyboard or any other storage device. • The data in the output stream can go to the screen or any other storage device. continue… Input device Output device Program Input Stream Output Stream Extraction from input stream Insertion into output stream 5By:-Gourav Kottawar
  • 6. Learners Support Publications www.lsp4you.com C ++ Stream Classes • The C++ 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 stream classes. • These classes are declared in the header file iostream. 6By:-Gourav Kottawar
  • 7. Learners Support Publications www.lsp4you.com C ++ Stream Classes ios istream ostreamstreambuf iostream istream_withassign iostream_withassign ostream_withassign continue… 7By:-Gourav Kottawar
  • 8. Learners Support Publications www.lsp4you.com C ++ Stream Classes ios istream ostreamstreambuf iostream istream_withassign iostream_withassign ostream_withassign continue… Provides the basic support for formatted and unformatted I/O operations. Provides the facilities for formatted and unformatted input Provides the facilities for formatted output Provides the facilities for handling both input and output streams. 8By:-Gourav Kottawar
  • 9. Learners Support Publications www.lsp4you.com Unformatted I/O Operations Overloaded Operators >> and << • The objects cin and cout are used for input and output of data of various types. • By overloading the operators >> and <<. • >> operator is overloaded in the istream class. • << operator is overloaded in the ostream class. • This is used for input data through keyboard. 9By:-Gourav Kottawar
  • 10. Learners Support Publications www.lsp4you.com Unformatted I/O Operations Overloaded Operators >> and << cin >> variable1 >> varibale2 ... >> variableN where variable1, variable2, …, variableN are valid C++ variable names. cout << item1 << item2 << … << itemN Where item1, item2, …,itemN may be variables or constants of an basic type. 10By:-Gourav Kottawar
  • 11. Learners Support Publications www.lsp4you.com Unformatted I/O Operations put( ) and get( ) Functions • get( ) and put( ) are member functions of istream and ostream classes. • For single character input/output operations. • There are two types of get( ) functions: – get(char*)  Assigns the input character to its argument. – get(void)  Returns the input character. • char c; cin.get(c) c = cin.get( ); – put( )  used to output a line of text, character by character. • char c; cout.put(‘x’); cout.put(c); 11By:-Gourav Kottawar
  • 12. Learners Support Publications www.lsp4you.com Unformatted I/O Operations getline( ) and write( ) Functions • getline( ) function reads a whole line of text that ends with a newline character. • cin.getline(line, size); • Reading is terminated as soon as either the newline character ‘n’ is encountered or size-1 characters are read. • write( ) function displays an entire line of text. • cout.write(line, size); • write( ) also used to concatenate strings. 12By:-Gourav Kottawar
  • 13. Learners Support Publications www.lsp4you.com Formatted Console I/O Operations C++ supports a number of features that could be used for formatting the output. These features include: – ios class functions and flags. – Manipulators. – User-defined output functions. 13By:-Gourav Kottawar
  • 14. Learners Support Publications www.lsp4you.com ios member functions width( )  to specify the required field size for displaying an output value. precision( )  to specify the number of digits to be displayed after the decimal point of a float value. fill( )  to specify a character that is used to fill the unused portion of a field. setf( )  to specify format flags that can control the form of output display. unsetf( )  to clear the flags specified. 14By:-Gourav Kottawar
  • 15. Learners Support Publications www.lsp4you.com Manipulators Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. To access manipulators, the file iomanip.h should be included in the program. – setw( ) – setprecision( ) – setfill( ) – setiosflags( ) – resetiosflags( ) 15By:-Gourav Kottawar
  • 16. Learners Support Publications www.lsp4you.com width( )  Defining Field Width To define the width of a field necessary for the output of an item. Since it is a member function, we have to use an object to invoke it. cout.width(w) Where w is the field width (number of columns). The output will be printed in a field of w characters wide at the right end of the field. 16By:-Gourav Kottawar
  • 17. Learners Support Publications www.lsp4you.com width( )  Defining Field Width The width( ) function can specify the field width for only one item – item that follows immediately. cout.width(5); cout << 543 << 12 <<“n”; cout.width(5); cout << 543; cout.width(5); cout << 12 << “n”; 5 4 3 1 2 5 4 3 1 2 The field should be specified for each item separately. 17By:-Gourav Kottawar
  • 18. Learners Support Publications www.lsp4you.com precision( )  Setting Precision • Used to specify the number of digits to be displayed after the decimal point while printing the floating-point numbers. • By default, the floating numbers are printed with six digits after the decimal point. • cout.precision(d); – Where d is the number of digits to the right of the decimal point. cout.precision(3); cout << sqrt(2) << endl; cout << 3.14159 << endl; cout <<2.50032 << endl; 1.141  truncated 3.142  rounded 2.5 o trailing zeros 18By:-Gourav Kottawar
  • 19. Learners Support Publications www.lsp4you.com precision( )  Setting Precision • Unlike width( ), precision ( ) retains the setting in effect until it is reset. • We can also combine the field specification with the precision setting. cout.precision(2); cout.width(5); cout << 1.2345; 1 . 2 3 19By:-Gourav Kottawar
  • 20. Learners Support Publications www.lsp4you.com fill( )  Filling and Padding • When printing values with larger field width than required by the values, the unused positions of the field are filled with white spaces, by default. • fill( ) function can be used to fill the unused positions by any desired character. cout.fill(ch); where ch represents the character which is used for filling the unused positions. cout.fill(‘ * ’); cout.wdith(10); cout << 5250 << endl; * * * * * 5 05 2* Like precision( ), fill( ) stays in effect till we change it. 20By:-Gourav Kottawar
  • 21. Learners Support Publications www.lsp4you.com setf( )  Formatting Flags, Bit-fields • When the function width( ) is used, the value will be printed right-justified in the created width. • setf( )function is used to print values in left- justified. • cout.setf(arg1, arg2) eg: cout.fill(‘ * ’); cout.setf(ios :: left, ios :: adjustfield); cout.wdith(10); cout << “TABLE 1” << endl; T A B L E * *1 * arg1 is formatting flags defined in the class ios and arg2 is bit field constant in the ios class. 21By:-Gourav Kottawar
  • 22. Learners Support Publications www.lsp4you.com setf( )  Formatting Flags, Bit-fields • When the function width( ) is used, the value will be printed right-justified in the created width. • setf( )function is used to print values in left- justified. • cout.setf(arg1, arg2) eg: cout.fill(‘ * ’); cout.setf(ios :: left, ios :: adjustfield); cout.wdith(10); cout << “TABLE 1” << endl; T A B L E * *1 * arg1 is formatting flags defined in the class ios and arg2 is bit field constant in the ios class. The formatting flag specifies the format action required for the output. Bit field specifies the group to which the formatting flag belongs. 22By:-Gourav Kottawar
  • 23. Learners Support Publications www.lsp4you.com Managing Output with Manipulators • The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats. • Some manipulators are more convenient to use than the member functions and flags of ios. • Two or more manipulators can be used as a chain in one statement. cout << manip1 << manip2 << maip3 << item; cout << manip1 << item1 << manip2 << item2; 23By:-Gourav Kottawar
  • 24. Learners Support Publications www.lsp4you.com Managing Output with Manipulators • Manipulators and their meanings cout << setw(5) << setprecision(2) << 1.2345 << setw(10) << setprecision(4) << sqrt(2); • We can jointly use the manipulators and the ios functions in a program. Manipulators Meaning Equivalent setw(int w) Set the field width to w width( ) setprecision(int d) Set floating point precision to d precision( ) setfill( int c) Set the fill character to c fill( ) 24By:-Gourav Kottawar
  • 25. Learners Support Publications www.lsp4you.com Manipulators & ios Member Functions • The ios member function return the previous format state which can be used later. • But the manipulator does not return the previous format state. cout.precision(2); // previous state. int p =cout.precision(4); // current state, p=2. cout.precision(p); // change to previous state 25By:-Gourav Kottawar
  • 26. Learners Support Publications www.lsp4you.com Designing Our Own Manipulators • We can design our own manipulators for certain special purposes. ostream & manipulator ( ostream & output) { ……… (code) return output; } ostream & unit (ostream & output) { output << “inches”; return output; } cout << 36 << unit;  will produce “36 inches”. 26By:-Gourav Kottawar
  • 27. Learners Support Publications www.lsp4you.com Designing Our Own Manipulators We can also create manipulators that could represent a sequence of operations: ostream & show (ostream & output) { output.setf(ios::showpoint); output.setf(ios::showpos); output << setw(10); return output; } This function defines a manipulator called show that turns on the flags showpoint and showpos declared in the class ios and sets the field width to 10. 27By:-Gourav Kottawar