The C++ standard libraries give a broad arrangement of input/output capabilities which we will see in ensuing sections. This part will examine extremely basic and most common I/O activities required for C++ programming.

C++ I/O happens in streams, which are successions of bytes. If bytes stream from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation and assuming bytes flow from main memory to a gadget like a showcase screen, a printer, a disk drive, or a network connection, etc., this is called output activity.

C++ I/O function


I/O Library Header Files

There are following header files essential to C++ programs −


Sr.No Header File & Function and Description
1

<iostream>

This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively.

2

<iomanip>

This file declares services useful for performing formatted I/O with so-called parameterized stream manipulators, such as setw and setprecision.

3

<fstream>

This file declares services for user-controlled file processing. We will discuss about it in detail in File and Stream related chapter.



The Standard Output Stream (cout)

The predefined object cout is a case of ostream class. The cout object is supposed to be "associated with" the standard output gadget, which usually is the showcase screen. The cout is utilized in conjunction with the stream addition operator, which is composed as << which are two less than signs as displayed in the accompanying example.



#include <iostream>
 
using namespace std;
 
int main() {
   char str[] = "Hello C++";
 
   cout << "Value of str is : " << str << endl;
}

At the point When the above code is aggregated and executed, it creates the following result −


Value of str is : Hello C++

The C++ compiler likewise decides the data type of variable to be output and chooses the suitable stream inclusion operator to show the value. The << operator is over-burden to output data items of built-in types integer, float, double, strings and pointer values.

The inclusion operator << might be utilized more than once in a solitary statement as displayed above and endl is used to add a new-line at the end of the line.


The Standard Input Stream (cin)

The predefined object cin is a case of istream class. The cin object is supposed to be appendend to the standard input device, which usually is the keyboard. The cin is utilized in conjunction with the stream extraction operator, which is composed as >> which are two more prominent than signs as displayed in the following example.



#include <iostream>
 
using namespace std;
 
int main() {
   char name[50];
 
   cout << "Please enter your name: ";
   cin >> name;
   cout << "Your name is: " << name << endl;
 
}

At the point When the above code is arranged and executed, it will incite you to enter a name. You enter a worth and then hit enter to see the following result −


Please enter your name: cplusplus
Your name is: cplusplus

The C++ compiler additionally decides the data type of the entered worth and chooses the suitable stream extraction operator to extricate the value and store it in the given factors.

The stream extraction operator >> might be utilized more than once in a solitary statement. To request more than one datum you can utilize the accompanying −


cin >> name >> age;

This will be equivalent to the accompanying two statements −


cin >> name;
cin >> age;

The Standard Error Stream (cerr)

The predefined object cerr is an occurance of ostream class. The cerr object is supposed to be connected to the standard blunder device, which is likewise a showcase screen however the object cerr is un-buffered and each stream inclusion to cerr makes its output to appear immediately.

The cerr is additionally utilized in conjunction with the stream insertion operator as displayed in the accompanying example.



#include <iostream>
 
using namespace std;
 
int main() {
   char str[] = "Unable to read....";
 
   cerr << "Error message : " << str << endl;
}

 At the point When the above code is arranged and executed, it produces the accompanying result −


Error message : Unable to read....

The Standard Log Stream (clog)

The predefined object clog is an occasion of ostream class. The clog object is supposed to be joined to the standard blunder gadget, which is additionally a display screen however the object clog is supported. This implies that every insertion to clog could make its output to be held in a support until the buffer is filled or until the cushion is flushed.

The clog is additionally utilized in conjunction with the stream insertion operator as displayed in the following model.



#include <iostream>
 
using namespace std;
 
int main() {
   char str[] = "Unable to read....";
 
   clog << "Error message : " << str << endl;
}

At the point When the above code is arranged and executed, it produces the accompanying result −


Error message : Unable to read....

You would not have the option to see any distinction in cout, cerr and clog with these little examples, but while composing and executing large programs the distinction becomes obvious. So it is great practice to show error messages utilizing cerr stream and while showing other log messages then clog should be utilized.


Previous                                                                         Next