2. CONSOLEI/O 2
Input and Output Stream
Que:
1. Explain input and output streams in C++.
2. Explain Input stream and Output stream in brief.
3. Describe briefly the features of I/O system supported by C++.
Ans:
I/O System in C++ is designed to work with variety of devices including terminals, disks
and tape drives.
I/O system supplies an interface to the programmer. This interface is known as stream.
A stream is a sequence of bytes.
Stream acts either as source from which the input data can be obtained or as a destination
to which the output data can be sent.
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.
A program extracts the bytes from an input stream and inserts bytes into an output stream.
Data in the input stream can come from the keyboard or any file. The data in the output
stream can go to the screen or any file.
Stream acts as an interface between the program and the Input/Output device.
C++ contains several predefined streams like: cin and cout
Cin represents the input stream connected with keyboard.
Cout represents the output stream connected to the screen.
3. CONSOLEI/O 3
C++ Stream Classes
Que:
1. Explain C++ Stream class hierarchy.
2. List out C++ steam classes.
Ans:
The C++ I/O system contains a hierarchy of classes. These classes are called stream
classes.
Stream classes are used to define various streams to deal with both the console and disk
file.
Fig. shows the hierarchy of the stream classes used for input and output operations with
the console unit.
ios class (Input/Output Stream)
- ios is the base class for istream (input stream) and ostream(output stream).
- ios class declares constants and functions that are required for handling formatted input and
output operations.
istream (Input Stream) class
- It is inherited from class ios. It declares input function such as get(), getline() and read().
ostream (Output Stream) class
- It is also inherited from class ios. It declares output functions such as put() and write().
4. CONSOLEI/O 4
iostream (Input/Output Stream)
- It is the Input/Output stream.
- It is inherited from istream and ostream and thus containing all the input/output operations.
- Two objects of this class cin and cout uses the overloaded operators ‘>>’ and ‘<<’.
Streambuf (Stream buffer)
- It provides interface to physical devices through buffers; it acts as a base for filebuf class used
in files.
Class Name Contents
ios (General
I/O stream
class)
Contains basic facilities that are used by all other input and output classes.
Also contains a pointer to a buffer object.
Declares constants and functions that are necessary for handling formatted
input and output functions.
istream (Input
stream)
Inherits the properties of ios.
Declares input functions such as get(), getline() andread()
Contains overloaded extraction operator >>
ostream
(output stream)
Inherits the properties of ios.
Declares output functions such as put() and write()
Contains overloaded insertion operator <<
iostream (I/O
stream)
Inherits the properties of ios,istream and ostream through multiple
inheritance and thus contains all the input and output functions.
streambuf Provides an interface to physical devices through buffers.
Acts as a base for filebuf class used ios files.
5. CONSOLEI/O 5
Unformatted I/O Operations
Que:
1. Explain the functions get() , put() , getline() with example.
Ans:
Overloaded Operators >> and <<
We have used the objects cin and cout for the input and output of data of various types.
Input and output is possible by overloading the operators >> and <<.
The ‘>>’ operator is overloaded in the istream class.
The ‘<<’ operator is overloaded in the ostream class.
The following is general format for reading data from the keyboard:
cin>>a>>b>>c;
The general form for displaying data on the screen is:
cout>>a>>b>>c;
Example:
#include <iostream.h>
int main()
{
int a;
cout<<"Enter the number";
cin>>a;
cout<<"The value of a="<<a;
return(0);
}
put() and get() functions
get() and put() are used to handle the single character input/output operations.
get() function:
There are two types of get() functions:
1. get(char *)
2. get(void)
1. get(char *)
It fetches a character including the blank space, tab and newline character.
It assigns the input character to its argument.
Example:
char ch;
cin.get(ch);
2. get(void)
It fetches a character including the blank space, tab and newline character.
It returns the input character.
6. CONSOLEI/O 6
Example:
char ch;
ch=cin.get();
put() function:
Put() function is a member of ostream class.
It can be used to putput a line of text character by character.
Example:
char ch=’A’;
cout.put(ch);
Example of get(char*), and put():
#include <iostream.h>
int main()
{
int a=65; char ch;
cin.get(ch);
cout.put(ch);
ch=cin.get();
cout.put(ch);
cout.put(a);
return 0;
}
getline() and write() functions
It is used to reads a whole line of text that ends with a newline character.
Syntax:
cin.getline (line, size);
First argument represents the name of string and second argument indicates the number of
character to be read.
Example:
char name[20];
int size=10;
cin.getline(name,size);
write()
It is used to display whole line of text on output device.
Syntax:
cout.write (line, size);
First argument represents the name of string and second argument indicates the number of
character to be display.
Example:
7. CONSOLEI/O 7
char name[20];
int size=10;
cout.write (name, size);
Example of getline() and write():
#include <iostream.h>
int main()
{
int size=5;
char name[50];
cin.getline(name,size);
cout.write(name,size);
return 0;
}