C++ Input/Output Stream with Examples

This chapter will talk about the most basic and common I/O operations that C++ programming needs. The C++ standard libraries offer a wide range of C++ input/output stream options, which we will explore later.

In addition, C++ I/O uses bytes-grouped streams. Input is when bytes flow from a keyboard, disk drive, or network connection to the main memory.

An output operation occurs when bytes move from main memory to a device such as a screen, printer, disk drive, or network connection.

To make sure the code in this lesson is correct, you must test it in your code editor. We have a free C Online Compiler for you to use if you’d rather run the code online.

What is the input and output stream in C++?

An input stream in C++ is used to get data from a source. An output stream is used to write data to a destination.

What are input and output streams in C++?

With C++’s libraries, there are many ways to handle input and output. In C++, input and output functions are done by sending and receiving a series of bytes, or File stream.

  • Input Stream: If a sequence of bytes moves from a device (like a keyboard) to the main memory, this is called an input stream.
  • Output Stream: When bytes move in the opposite direction, from the main memory to the device (display screen), this is called output.

I/O Library Header Files

Header files are important parts of C++ programs.

Header FilePurpose and Description
<iostream>This iostream class defines the objects cin, cout, cerr, and clog: the standard input stream, the standard output stream, the unbuffered standard error stream, and the buffered standard error stream.
<iomanip>This file defines services that can be used for formatted I/O with so-called parameterized stream objects manipulators like setw and set precision.
<fstream>This file lists services for file processing that the user controls. We’ll discuss it in more detail in the chapter about Files and Streams.

Standard Input Stream (cin)

Cin utilizes istream. cin uses the keyboard for input. Moreover, cin is used with a stream insertion operator (two greater than signs). Below is an example of cin with a stream extraction operator.

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

When the code above is put together and run, it will ask you for a name. When you type in a number and hit enter, you’ll see the following:

Please enter your name: cplusplus
Your name is: cplusplus

Standard Output Stream (cout)

The cout object uses the ostream class. cout is “attached” to the display screen’s normal output device. It is also used with the stream extraction operator, two less than signs ().

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

The following is what happens when the code above is compiled and run:

Value of str is : Hello C++

Standard Error Stream (cerr)

The cerr objects are defined ostream. It’s reported that the cerr object is associated with the standard error device, a display screen. Each stream insertion to Cerr causes its output to appear immediately because it’s not buffered.

As shown in the following example, the cerr can also be used with the stream insertion operator.

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

When the code above is put together and run, the following happens: the error message shows.

Error message : Unable to read....

Standard Log Stream (clog)

The object clog, which is already set up, is an instance of the ostream class. The clog object is said to be connected to the standard error device, which is also a display screen, but the object clog is buffered.

This means that each insertion to clog could cause its output to be held in a buffer until the buffer is full or until the buffer is flushed.

As shown in the following example, the clog can also be used with the stream insertion operator.

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

The following is what happens when the code above is compiled and run:

Error message : Unable to read....

With these small examples, you wouldn’t be able to tell the difference between cout, cerr, and clog. However, when you write and run big programs, the difference is clear.

So it’s a good idea to use the cerr stream to show error messages and the clog stream to show other log messages.

Summary

C++ Input/Output Stream tutorial covers all the essentials in learning the basic meaning of input and output streams and the libraries and the standard streams.

Leave a Comment