The Definition of Iostream in C++

Jinku Hu Oct 12, 2023
The Definition of Iostream in C++

This guide discusses how to utilize the basic input/output library in C++.

Utilize the <iostream> Header to Include Global Stream Objects in C++

The Input/Output library is the core part of the C++ STL utilized by almost every real-world program. The C++ I/O operations are abstracted in the form of streams, which can be thought of as generic data sequences. A stream abstraction is implemented using multiple inherited classes and is exposed to the user as different named objects specialized for certain I/O operations. Note that the stream-based I/O library as a whole can be used to print text to console or read/write data from files on the disk. You can think of the streams library as the primary means for the C++ program to interact with the file system and devices.

The two essential stream classes are istream and ostream, corresponding to the streams used to read and write the data. These classes provide operations on character streams. There are four global stream objects frequently utilized by programs to conduct operations like taking user input or printing the error messages. These objects are:

  • cout: it represents a standard output stream corresponding to the stdout in C.
  • cin : it’s the standard input stream (corresponds to stdin).
  • cerr: it’s the standard error stream implemented as unbuffered stream (corresponds to stderr).
  • clog: it’s the standard logging stream that does not have the C equivalent, and it offers the buffered version of cerr.

Another important part of the I/O stream library is operators that are used to chain the operations between different objects. The << operator, called stream insertion operator, pushes the data into a stream object. On the other hand, the >> operator is known as the stream extractor. Note that these operators generally conduct shift operations on arithmetic types, but they are implemented as overloaded operators in this case.

In the following example, we show you the basic use of the C++ stream-based I/O classes. We use the cout object to output the string contents to the console. Since the cout corresponds to a standard output stream, we use the insertion operator to push the data in, and it gets printed to the console usually. Notice that endl object is called a manipulator that outputs a newline character and flushes the output buffer.

Stream manipulators come in various types, there are the ones that modify the input/output like endl, and there are ones that force the stream object to interpret the input/output differently. The latter ones include the number base manipulators like: dec, hex, and oct.

#include <iostream>
#include <string>

using std::cerr;
using std::cout;
using std::endl;
using std::string;

int main() {
  string str1("what does iostream mean?");

  cout << str1 << endl;
  cout << str1 << " - " << str1 << endl;

  return EXIT_SUCCESS;
}

Output:

what does iostream mean?
what does iostream mean? - what does iostream mean?

As shown in the previous program, the most powerful feature of stream operators and objects is to be chained sequentially multiple times. In this case, we see that we can combine the literal values with other variables to push all of them to a cout stream. Next, we employ the cin and cerr objects to read the user input and print the read data.

Note that the object of the type that needs to be read should be declared in advance as the float number x. Another useful property of insertion/extraction operators is that they return the non-zero value if the operation failed. Alternatively, you can use the member functions fail and bad to check if the given stream object encountered specific errors.

#include <iostream>
#include <string>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
  float x;

  cout << "enter float: ";
  if (!(cin >> x)) {
    cerr << "error while reading the floating point value!" << endl;
    return EXIT_FAILURE;
  }
  cout << x;

  return EXIT_SUCCESS;
}

Output:

error while reading the floating point value!
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ IO