CE144
OBJECT ORIENTED PROGRAMMING
WITH C++
UNIT-10
Managing Console I/O Operations
N. A. Shaikh
[email protected]
Topics to be covered
Introduction
C++ stream
C++ stream classes
Unformatted console I/O Operations
Formatted console I/O Operations
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
2
Introduction
Every program takes some data as input and generates
processed data as output following the familiar input-
process-output cycle.
We have used cin and cout with the operators >> and
<< for the I/O operations.
C++ supports a rich set of I/O functions and operations.
C++ use the concept of stream and stream classes to
implement it I/O operations with the console and disk
files
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
3
C++ Streams
The I/O system supplies an interface to the
programmer that is independent of the actual device
being accessed. This interface is known as stream.
A stream is a sequence of bytes.
The source stream that provides data to the program is
called the input stream.
The destination stream that receives output from the
program is called the output stream.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
4
C++ Streams
The data in the input stream can come from the
keyboard or any other device.
Similarly, the data in the output stream can go to the
screen or any other storage device
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
5
C++ Streams
C++ contains several pre-defined streams that are
automatically opened when a program begins its
execution.
Ex: cin represents the input stream connected to the
standard input device(usually keyboard) and cout
represents the output stream connected to the
standard output device(usually screen)
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
6
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
which should be included in all the programs that
communicate with the console unit.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
7
C++ Stream classes
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
8
C++ Stream classes
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
9
Unformatted console I/O Operations
Overloaded Operators >> and <<
put() and get() functions
getline() and write() functions
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
10
Overloaded Operators >> and <<
The objects cin and cout are used for the input and
output of data of various types by overloading >> and
<< operators.
The >> operator is overloaded in the istream class and
<< is overloaded in the ostream class
General format for cin and cout:
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
11
Overloaded Operators >> and <<
Notes on >>
>> will cause the computer to stop the execution and
look for input data from the standard input device.
Spaces, newlines and tabs will be skipped.
The reading for a variable will be terminated at
• A white space OR
• A character that does not match the destination
type
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
12
Overloaded Operators >> and <<
Suppose user enters 8524D, then ‘D’ remains in the input
stream and will be input to the next cin statement.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
13
put() and get() functions
get() and put() are member functions of istream and
ostream classes.
For single character input/output operation including
blank space , tab and newline character.
There are two types of get() functions:
• get(char*)->Assigns the input character to its
argument
• get(void)->returns the input character
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
14
put() and get() functions
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
15
put() and get() functions
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
16
put() and get() functions
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
17
getline() and write() functions
The getline() function reads a whole line of text that
ends with a newline character.
The reading is terminated as soon as either the new
line character is read or size-1 characters are read.
write() function displays an entire line of text
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
18
getline() and write() functions
getline() & write() cin & cout
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
19
getline() functions
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
20
write() functions
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
21
write() functions
It is possible to concatenate two strings using the write()
function.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
22
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
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
23
ios class functions and flags
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
24
Defining field width: width()
The width() function is used to define the width of a
field necessary for the output of an item
The output will be printed in a field of w characters
wide at the right end of the field
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
25
Setting Precision: precision()
We can 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.
NOTE: Unlike, the function width(), precision() retains the
setting in effect until it is reset.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
26
Filling and Padding: fill()
The unused positions of the field are filled with white
spaces.
However, the fill() function can be used to fill the
unused positions by any desired character
NOTE: Like precision(), fill() stays in effect till we change it
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
27
Formatting Flags, Bit-fields and setf()
The setf() (set flags) member function of the ios class is
used for various types of formatting
The arg1 is one of the formatting flags, specifying the
action required for the output.
The arg2 known as bit field specifies the group to which
the formatting flag belongs.
There are three bit fields and each has a group of
format flags
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
28
Formatting Flags, Bit-fields and setf()
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
29
Formatting Flags, Bit-fields and setf()
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
30
Displaying trailing zeros and Plus sign
The setf() can be used with the flag as a single
argument for achieving various format of output.
There are some flags that do not have bit fields
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
31
Displaying trailing zeros and Plus sign
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
32
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.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
33
Manipulators
NOTE: Refer Unit-3 for more details.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
34
Manipulators
We can jointly use the manipulators and the ios
functions
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
35
Manipulators VS ios Member Function
The ios member function return the previous format
state which can be used later.
But the manipulator does not return the previous
format state.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
36
Manipulators VS ios Member Function
Difference between ios member functions and
manipulators
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
37
User-defined output functions/ Manipulators
The programmer can also define his own manipulator
according to the requirement of the program.
Syntax:
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
38
User-defined output functions/ Manipulators
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
39
User-defined output functions/ Manipulators
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
40
Let’s Practice
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
41
Let’s Practice
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
42
Let’s Practice
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
43
End of Unit-10