Input Output in C++ – (cout, cin, cerr, clog)

Every C++ newbie have come across input output objects (i.e. cin & cout) and what was there functionality etc. But the most important thing is these two operations are not part of the core C++ language. These cin & cout functionalities are defined in the C++ standard library (resides in the std namespace).

While learning the language you included the iostream library to make use of the cin and cout objects for Input/output operations. C++ provide many such libraries various task. In C++ input output is performed in the form of sequence of bytes known as streams.

Streams in C++

A stream is a sequence of bytes that can be accessed sequentially and may produce or consume unlimited amounts of data. In C++ we mostly come across two streams input and output.

Input stream which holds the input data from user such as keyboard, file etc. and wait in buffer until the program ready to execute it.

Similarly, the Output stream holds the data from output devices until they are ready to accept that data, output devices includes monitor, printer etc.

Important: Buffer is temporary placeholder many programming languages in memory (ram/disk) on which data can be dumped and then processing can be done. Buffer increases the performance of the computer by allowing read/write operation in larger chunks.

Unbuffered output is addressed immediately, whereas buffered output is stored and passed out to output console as whole data sequence. The C++ standard libraries provide an extensive set of input output objects. In this chapter will discuss very basic and most common I/O operations required for C++ programming.

Before we start learning about object that overlook stream flow, let’s have a look at header files that hold definition for all object responsible for input/output operations.

Input Output Library Header Files in C++

Iostream Header file defines the input/output stream objects such as cin, cout, cerr, clog which corresponds to standard I/O stream, unbuffered and buffered error stream.

Standard Input Output streams in C++

C++ comes with four predefined standard stream objects in libraries which comes handy for our use. Including <iostream> automatically includes also <ios>, <streambuf>, <istream>, <ostream>and <iosfwd>.

The Standard Input Stream (cin)

The Object cin is an instance of istream class. cin tied to the standard input devices like keyboard. The cin object is used with the stream extraction operator (<<).

#include <iostream>
 
using namespace std;
 
int main() {
   char str[50];
 
   cout<<"Say Hello to the WORLD: ";
   cin>> str;
   cout<<str<<endl;


return 0;
 
}

Output:

Say Hello to the WORLD: Hello-World!
Hello-World!

In above program you might have noticed I gave input separated by hyphen because if I enter them separated by space only one word will enter the buffer and get execute, this will be explained in buffered and unbuffered inputs in separate lesson.

The advantage of C++ is we don’t need to worry about the data type of input from user, this work is done by C++ compiler automatically selecting the appropriate stream extraction operator (>>) to extract the value and store it in the given variables.

Similarly, more than one input can be given as given below.

cin>> var1 >> var2;

The Standard Output Stream (cout)

The object cout is an instance of ostream class. cout tied to the standard output devices like monitor which displays the output. The cout object is used with the stream insertion operator (<<).

#include <iostream>
 
using namespace std;
 
int main() {
char str[] = "Hello World!";
   
cout <<str<<endl;

return 0;
}

Output:

Hello World!

In C++ you don’t need to worry about the output data type, compiler automatically determines the data type of output variable ‘str’ and use the appropriate stream insertion operator to display the value. The << insertion operator is overloaded to output the data of built-in types integer, float, double, strings and pointer values.

The insertion operator << may be used more than once in a single statement as shown above and endl is used to add a new-line at the end of the line.

The Un-Buffered Standard Error Stream (cerr)

The object cerr is an instance of ostream class and it is tied to the standard error device, which display error message on the screen. Object cerr is an un-buffered so each stream inserted displayed immediately on output device. The cerr is used in combination with the stream insertion operator (<<) as shown in the following example.

#include <iostream>
 
using namespace std;
 
int main() {
 
   cerr<<"Error occurred!"<<endl;


return 0;
}

Output:

Error occurred!

The Standard Log Stream (clog) – Buffered error stream

The object clog is an instance of ostream class and it is tied to the standard error device, which display error message on the screen but the object clog is buffered. This means that each insertion to clog will be held in a buffer until the buffer is filled or until the buffer is flushed.

The clog is used in combination with the stream insertion operator (<<) as shown in the following example.

#include <iostream>
 
using namespace std;
 
int main() {
 
   clog<<"Error Occurred!"<<endl;

return 0;
}

Output:

Error Occurred!

If you look at above three examples of cout, cerr, clog there is no significant difference between them. To find it in practice you need to write big programs. So it is good practice to display error messages using cerr and while displaying other log messages then clog should be used.

There are many library header file for input output in C++ such as <fstream>, <iomanip>, <ios>, <iosfwd> , <iostream>, <istream>, <ostream>, <sstream>. <streambuf> which will be explained in further lessons.

Till than Learn, Subscribe and give you valuable feedback!

Leave a Reply

Your email address will not be published. Required fields are marked *