Professional Documents
Culture Documents
CE144 OOPC Unit-10
CE144 OOPC Unit-10
UNIT-10
Managing Console I/O Operations
N. A. Shaikh
nishatshaikh.it@charusat.ac.in
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
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.
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 <<
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 <<
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.
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.
Manipulators.
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
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.
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
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
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
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.
Unit 10: Managing Console I/O Operations Prepared By: Nishat Shaikh
36
Manipulators VS ios Member Function
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