Module - 36 C++ I/O System Basics
Module - 36 C++ I/O System Basics
Module - 36
1. Introduction
2. Stream classes of C++
3. Predefined Standard Input/Output Streams
4. Functions of <istream> class
5. Functions of <ostream> class
6. Summary
Learning outcome –
After studying this module, you will be able to:
1. Learn about Stream classes of C++
2. Know about Predefined Standard Input/Output Streams
3. Study about Functions of <istream> class
4. Study about Functions of <ostream> class
1. Introduction
In C++, the Input/output is offered as a component of the standard library. The C++ standard
libraries present widespread set of input/output facilities which are most commonly used for I/O
operations. The Input/output in C++ language mostly occurs in streams, which are nothing but
sequences of bytes. In general, the streams perform two types of operations, they are Input
operation and Output operation.
ios
iostream
The istream and ostream classes are in turn base classes for the class iostream, i.e.,
Input/Output Stream.
The stream classes and their functionality can be represented in the table as follows:
Syntax:
cin >> variable1 >> varibale2 ... >> variableN;
Example:
cin>>rollno>>name>>total;
Program to illustrate the usage of cin object and extraction operator (>>)
#include<iostream>
using namespace std;
int main()
{
int number1 ;
double number2 ;
char ch1 ;
char Grade [30];
cout <<"Enter value of Number1 : " ; cin >>number1;
cout <<"Enter the value of Number2 : "; cin >>number2;
cout <<"Enter the value of ch1 : "; cin>>ch1 ;
cout<<"Enter a Grade : "; cin >> Grade ;
cout <<"The data that is entered is "<<endl;
cout <<"Number1 = "<<number1 <<" , Number2 = "<<number2 <<" , ch1 = "<<ch1
<<", Grade = " <<Grade <<endl;
}
When the above code is compiled and executed, it produces the following result:
Enter value of Number1 : 10
Enter the value of Number2 : -20
Enter the value of ch1 : Z
Enter a Grade : Excellent
The data that is entered is
Number1 = 10 , Number2 = -20 , ch1 = Z , Grade = Excellent
Syntax:
cout << item1 << item2 << … << itemN;
Example:
cout<<Nameofcustomer<<" paid rupees "<<Price<<" for "<< Custno <<" Mangoes "
<<endl;
Program to illustrate the use of cout object and insertion operator (<<)
#include<iostream>
using namespace std;
int main()
{
int Custno =401;
double Price =1000.5;
char flag = 'F';
char Nameofcustomer[] = "Deepali";
cout << "flag is "<<flag<<endl;
cout<<Nameofcustomer<<" paid rupees "<<Price<<" for "<< Custno <<" Mangoes "
<<endl;
}
When the above code is compiled and executed, it produces the following result:
flag is F
Deepali paid rupees 1000.5 for 401 Mangoes
C and C++ Programming
Electronic Science
36. C++ I/O System Basics
6
When the above code is compiled and executed, it produces the following result:
3.4 clog
The clog which is a predefined object is an instance of ostream class which represents the
standard logging stream. The clog object is said to be connected to the standard error device,
which is a display screen, but because, the predefined object clog is buffered, therefore, each
insertion to clog might effects its output to be seized in a buffer until the buffer is full or until the
buffer is flushed. The clog object can also be used in combination with the stream insertion
operator. It is linked with the cstdio stream stderr, like cerr. By default, the majority of the
systems have their normal error and logging output which is mostly going to the console, where
the text messages are displayed, even though this can usually be forwarded. Since, clog is a
predefined object of ostream class , it is possible to write characters to it either as formatted data
with the help of the insertion operator (<<) or as unformatted data with the help of the
write member function.
#include <iostream>
using namespace std;
void test( )
{
int n = 0;
cout << "Enter a number: "<<endl;
cin >>n;
cerr << "test for cerr" << endl;
clog << "test for clog" << endl;
}
int main( )
{
int i = 0;
cout << "Enter a number: "<<endl;
cin >> i;
cerr << "test for cerr" << endl;
clog << "test for clog" << endl;
test( );
}
When the above code is compiled and executed, it produces the following result:
Enter a number: 3
test for cerr
test for clog
Enter a number: 2
test for cerr
test for clog
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int number ;
cout<<"Enter a number : "; cin>>number;
if (number<0)
{
cout<<"The value is negative\n"<<endl;
C and C++ Programming
Electronic Science
36. C++ I/O System Basics
8
When the above code is compiled and executed, it produces the following result:
Enter a number : 36
Square root of number 36 is 6
Square root of 35 is 5.91608
The square root of 25 is : 5
#include <iostream>
using namespace std;
class Test
{
int number;
Test()
{
clog << "static constructor\n";
}
~Test()
{
clog << "static destructor\n";
}
};
Test t1; // static object
int main()
{
clog << "main function\n";
}
When the above code is compiled and executed, it produces the following result:
static constructor
main function
C and C++ Programming
Electronic Science
36. C++ I/O System Basics
9
static destructor
a) get()
The get() function is a member function of istream class is mainly used to read a single character
from an input device. The different ways of using get() function are:
Example:
cin.get();
char ch1;
ch1=cin.get()
#include<iostream>
using namespace std;
int main ()
{
char ch1;
cout <<"Enter a character : ";
ch1 = cin.get();
cout <<"The character that is entered is = "<< ch1<< endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Enter a character : Z
The character that is entered is = Z
Example:
char ch1;
cin.get(ch1);
#include<iostream>
using namespace std;
int main()
{
char ch1;
cout <<"Enter a character : ";
cin.get(ch1);
cout <<"The Character that is entered is "<< ch1;
cout<<"\n";
return 0;
}
When the above code is compiled and executed, it produces the following result:
Enter a character : Z
The character that is entered is = Z
Syntax:
cin.get(char*buffer, size_t n);
Example:
char Name[20];
cin.get(Name,6);
Example:
char Buffer [ ]= "Example of getline function in CPP";
char ch1=‘#’;
cin.getline( Buffer, 15,ch1);
When the above code is compiled and executed, it produces the following result:
Enter a Text1 : this is my first program in cpp on i/o #basics
C and C++ Programming
Electronic Science
36. C++ I/O System Basics
12
b) getline()
The getline() function works like that of get() function, i.e., it tries to add a null character after
the line in the character array. It eliminates the delimiter from the stream, but it does not store it
in the character array. It is must that the getline() function has to be used with object of istream
class and it is mainly useful to read a complete line. The getline() function can have two or three
arguments.
Syntax:
getline (char* s, streamsize n );
getline (char* s, streamsize n, char delimeter);
Example:
char Buffer [ ]= "Example of getline function in CPP";
char ch1=‘#’;
cin.getline( Buffer, 20);
cin.getline( Buffer, 15,ch1);
c) ignore()
The ignore() function reads and it tries to rejects up to n characters or up to the reading of
delimiter character or end of file whichever occurs first. The default value of n is 1.
Syntax:
cin.ignore (streamsize n, int delimiter)
{
char firstname, lastname;
cout<<"Enter first name and last name: ";
firstname = cin.get();
cin.ignore(30,' ');
lastname = cin.get();
cout<<"The initial letters are: "<<firstname<<lastname<<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Enter first name and last name: Vivian Richards
The initial letters are: VR
d) peek()
The peek() function returns the next character from an input stream, but it does not take out the
character from the stream.
Example:
cin.peek('H');
e) putback()
The putback() function tries to return the formerly read character to the input stream.
Syntax:
putback (char c);
Example:
char ch = ‘D’;
cin.peek(‘F’);
cin.putback (ch);
}
}
When the above code is compiled and executed, it produces the following result:
Enter a sentence : This Basket contains 1000 Apples
This Basket contains 1000 Zpples
f) read()
The read() function is a member function of istream class and is mainly used with input streams
such as cin.read(). It comes under the category of unformatted Input function. The read()
function does enter into the memory some bytes without any formatting.
Syntax:
read (char* s, streamsize n);
Example:
char Text[15];
cin.read(Text ,15);
g) gcount()
The gcount() is a member function of istream class which tries to return the number of
unformatted characters that is last extracted.
Syntax:
streamsize gcount() const;
When the above code is compiled and executed, it produces the following result:
Enter Text : This is my first program on gcount function
The text that is entered is
a) put()
The put() function is a member function of ostream class. It is mainly used to write a character to
output device. The put() function is used with an object of ostream class.
Syntax:
put (char c);
Example:
cout.put(ch1);
b) write()
The function write() is a member function of ostream class and is mainly used with output
streams such as cout.write(). It comes under the category of unformatted Output function. The
write() function output of a number of bytes from the character array in the memory without any
formatting.
Syntax:
write (const char* s, streamsize n);
Example:
char Text[30];
cout.write (Text, 30)
5. Summary
C++ includes a hierarchy of classes called as Stream classes that are mainly used to
describe various streams to deal with console and disk files.
The stream classes are stated in the iostream or iostream.h header file.
The ios class is the base class for the istream and ostream classes.
The istream and ostream classes are in turn base classes for the class iostream, i.e.,
Input/Output Stream.
The ios class offers the basic services that are used by all input and output classes.
The istream class declare the input functions such as get(), getline(), read() and it also
include overloaded extraction operator (>>).
The ostream class declare the input functions such as get(), getline(), read() and it also
include overloaded extraction operator (>>).
The streambuf class also provides the methods of the class for loading the contents of the
buffer, access the contents of the buffer, flush the contents of the buffer, organizing the
memory of the buffer.
The filebuf class tries to present the necessary and basic operations that are related to
files.
The strstreambuf class is mainly designed to hold the buffers that are related to memory.
The cin is mainly used to get the input from standard input device, such as keyboard.
The cout is mainly used to display the output to standard output device, such as monitor
or screen.
The cerr is mainly used for standard error to display on monitor or screen.
The clog is mainly used for buffered error to display on monitor.
The cin object object of istream class along with extraction operator ( >>) is mainly used
for the input of primary types.
It is possible with the cin object to overload the extraction operator (>>) for the user
defined types, but it can be used for the class of input object.
The cout object of ostream class is used for standard output along with overloaded
insertion operator <<.
The cout object can be used for user defined data, but it is must that the overloading
functions for operator << are to be described by the programmer for the class of the
objects being that are dealt with.
The cerr object is said to be “connected” to the standard error device, which is a display
screen or monitor, but, since, the cerr object is un-buffered, therefore, each stream
insertion to cerr causes its output to come into view instantly.
The cerr object can also be used in combination with the stream insertion operator.
The clog object is said to be connected to the standard error device, which is a display
screen, but because, the predefined object clog is buffered, therefore, each insertion to
clog might effects its output to be seized in a buffer until the buffer is full or until the
buffer is flushed.
The get() function is a member function of istream class is mainly used to read a single
character from an input device.
The get() function with no arguments tries to enter one character from the selected
streams including whitespace and it tries to return the character as the value of the
function call.
The get() function with a character argument tries to enter the next character from the
input stream including whitespace.
The get() function with two arguments tries to read first n-1 characters one after another
into the buffer.
When the get() function is used with three arguments, then, if the delimiter character is
met, then the function stops reading the further characters, even though the number (n –
1) is not reached.
The getline() function eliminates the delimiter from the stream, but it does not store it in
the character array.
The ignore() function reads and it tries to rejects up to n characters or up to the reading of
delimiter character or end of file whichever occurs first.
The peek() function returns the next character from an input stream, but it does not take
out the character from the stream.
The putback() function tries to return the formerly read character to the input stream.
The read() function does enter into the memory some bytes without any formatting.
The gcount() is a member function of istream class which tries to return the number of
unformatted characters that is last extracted.
The put() function is mainly used to write a character to output device.
The write() function output of a number of bytes from the character array in the memory
without any formatting.