0% found this document useful (0 votes)
20 views

Chapter-10 - OOPC IO Operation

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Chapter-10 - OOPC IO Operation

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter-10 Managing Console

I/O Operations

Prepared By: Aayushi Chaudhari


Assistant Professor, CE,CSPIT.

27 May 2022| U & P U. Patel Department of Computer Engineering 1


Contents
Introduction
C++ stream
C++ stream classes
Unformatted console I/O Operations
formatted console I/O Operations

Weightage: 5%
Hours: 2 Hours needed

27 May 2022| U & P U. Patel Department of Computer Engineering


Introduction to I/O
• Every program takes some data as input and generates processed data as an
output following the familiar input-process-output cycle.
• It is essential to know how to provide the input data and present the results in
the desired form.
• The use of the cin and cout is done with the operator >> and << for the input
and output operations.
• C++ supports a rich set of I/O functions and operations.
• It uses the concept of stream and stream classes to implement its I/O
operations with the console and disk files.
27 May 2022| U & P U. Patel Department of Computer Engineering
C++ Streams
• A stream is a sequence of bytes, also known as buffer.
• stream refers to the stream of characters that are transferred between the
program thread and I/O.
• The source stream that provides the data to the program is called the input
stream.
• The destination stream that receives output from the program is called the
output stream.
• The data in the input stream can come from the keyboard or any other input
device.
• The data in the output stream can go to the screen or any other output device.
27 May 2022| U & P U. Patel Department of Computer Engineering
C++ Streams cont..
• C++ contains several pre-defined streams that are automatically opened when a program begins its execution.

• These include cin and cout.


• cin represents the input stream connected to the standard input device (usually the keyboard) and
• cout represents the output stream connected to the standard output device (usually the screen).
• Note: “stream output” is just another way of saying “to write to a stream” and “stream input” means “to read
from a stream”.

27 May 2022| U & P U. Patel Department of Computer Engineering


Process involved in writing to a stream
• The stream std::cout is connected to the computer’s display, usually called console output.
• When a statement such as std::cout << x; is executed, the value of variable x is converted
into a sequence of characters (corresponding to its text representation).
• For instance, if variable x is a double storing 12.347, then this value is converted to the
sequence of characters '1''2''.''3''4''7’.
• These characters are then inserted into the stream’s buffer for transmission to the output
device, i.e. the computer’s display.
• The conversion of variable x’s value to text can be controlled by the programmer by using
C++ manipulators, like in std::cout << std::fixed << std::setprecision(2) << x;

27 May 2022| U & P U. Patel Department of Computer Engineering


Advantage of using Stream
• Firstly, a variable’s value is converted to text, i.e. a sequence of characters;
• secondly, the transmission of the contents of a stream’s buffer to the output device (like a
computer’s display) occurs automatically.

27 May 2022| U & P U. Patel Department of Computer Engineering


C++ Stream Classes
• ios is the base class.
• The iostream class is derived from istream and ostream classes.
• The ifstream and ofstream are derived from istream and ostream,
respectively.
• These classes handles input and output with the disk files.
• The fstream.h header file contains a declaration of ifstream,
fstream and fstream classes.
• The iostream.h file contains istream, ostream and iostream classes and
included in the program while doing disk I/O operations.
27 May 2022| U & P U. Patel Department of Computer Engineering
Stream class hierarchy

27 May 2022| U & P U. Patel Department of Computer Engineering


C++ Stream Classes
• The filebuf class contains input and output operations with files.
• The streambuf class does not organize streams for input and output
operations, only derived classes of streambuf performs I/O
operations.
• These derived classes arranges a space for keeping input data and
for sending output data.
• The istream and ostream invokes the filebuf functions to perform
the insertion or extraction on the streams.
27 May 2022| U & P U. Patel Department of Computer Engineering
I/O Stream Meaning Description
istream Input Stream It reads and interprets input.

ostream Output stream It can write sequences of characters and represents other kinds of data.

ifstream Input File Stream The ifstream class is derived from fstreambase and istream by multiple inheritance.

This class accesses the member functions such as get(), getline(), seekg(), tellg()
and read().

It provides open() function with the default input mode and allows input operations.

ofstream Output File Stream The ofstream class is derived from fstreambase and ostream classes.
This class accesses the member functions such as put(), seekp(), write() and
tellp().

It provides the member function open() with the default output mode.

fstream File Stream The fstream allows input and output operations simultaneous on a filebuf.

It invokes the member function istream::getline() to read characters from the file.

This class provides the open() function with the default input mode.

fstreambase File Stream Base It acts as a base class for fstream, ifstream and ofstream. The open() and close()
functions are defined in fstreambase.

27 May 2022| U & P U. Patel Department of Computer Engineering


Advantages of Stream Classes
• Stream classes have good error handling capabilities.
• These classes work as an abstraction for the user that means
the internal operation is encapsulated from the user.
• These classes are buffered and do not uses the memory disk
space.
• These classes have various functions that make reading or
writing a sequence of bytes easy for the programmer.
27 May 2022| U & P U. Patel Department of Computer Engineering
Unformatted input/output operations In C++
• Cin and cout objects are used for input and output operations of data
of varied types, by overloading >> and << operators.
• The operator >> is overloaded in the istream class.
• The operator << is overloaded in the ostream class.

cin >> var1 >> var2 >> …. >> var_n;


Cout<<var1<<var2<<….<<var_n;

27 May 2022| U & P U. Patel Department of Computer Engineering


Unformatted input/output operations In C++
• Here, var1, var2, ……, varn are the variable names that are declared already.
• The input data must be separated by white space characters and the data type
of user input must be similar to the data types of the variables which are
declared in the program.
• The operator >> reads the data character by character and assigns it to the
indicated location.
• Reading of variables terminates in two cases:
❖ when white space occurs
or
❖ character type occurs that does not match the destination type
27 May 2022| U & P U. Patel Department of Computer Engineering
Example

27 May 2022| U & P U. Patel Department of Computer Engineering


put() and get() functions:
• The class istream and ostream have predefined functions
get() and put(), to handle single character input and output
operations.
• The function get() can be used in two ways, such as
get(char*) and get(void) to fetch characters including
blank spaces, newline characters, and tab.
• The function get(char*) assigns the value to a variable
and get(void) to return the value of the character.
27 May 2022| U & P U. Patel Department of Computer Engineering
Example

27 May 2022| U & P U. Patel Department of Computer Engineering


Getline() and write()
• The function getline() and write() provide a more efficient way to handle line-oriented
inputs and outputs.
• getline() function reads the complete line of text that ends with the new line character.
This function can be invoked using the cin object.
cin.getline(variable_to_store_line, size);
The reading is terminated by the ‘\n’ (newline) character.
The new character is read by the function, but it does not display it, instead, it is replaced with
a NULL character.
After reading a particular string the cin automatically adds the newline character at end of the
string.
The write() function displays the entire line in one go and cout object is used to invoke it.

27 May 2022| U & P U. Patel Department of Computer Engineering


Write()
cout.write(variable_to_store_line, size);

• The key point to remember is that the write() function does not stop displaying the string
automatically when a NULL character occurs. If the size is greater than the length of the
line then, the write() function displays beyond the bound of the line.

27 May 2022| U & P U. Patel Department of Computer Engineering


Example

27 May 2022| U & P U. Patel Department of Computer Engineering


Formatted console I/O Operations
Various features are supported in C++, that can be used for formatting the output.

Following ways are available for formatting the Output:


standard ios class functions
Formatting using Manipulators
User Defined output functions

27 May 2022| U & P U. Patel Department of Computer Engineering


ios class functions
Few standard ios class functions are:
1. width(): The width method is used to set the required field width. The output will be
displayed in the given width
2. precision(): The precision method is used to set the number of the decimal point to a float
value
3. fill(): The fill method is used to set a character to fill in the blank space of a field
4. setf(): The setf method is used to set various flags for formatting output
5. unsetf(): The unsetf method is used To remove the flag setting

27 May 2022| U & P U. Patel Department of Computer Engineering


Examples

27 May 2022| U & P U. Patel Department of Computer Engineering


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.

27 May 2022| U & P U. Patel Department of Computer Engineering


27 May 2022| U & P U. Patel Department of Computer Engineering
User Defined Output Functions
• C++ allows the programmer to define their own function.
• A user-defined function groups code to perform a specific task and that
group of code is given a name (identifier).
• When the function is invoked from any part of the program, it all
executes the codes defined in the body of the function
returnType functionName (parameter1, parameter2,...)
{
// function body
}
27 May 2022| U & P U. Patel Department of Computer Engineering
Example

27 May 2022| U & P U. Patel Department of Computer Engineering


Thank You.

27 May 2022| U & P U. Patel Department of Computer Engineering

You might also like