stringstream

What Are ostream and istream in C++?

What are ostream and istream in C++? They are the standard classes for output and input, and cout, cin and the stringstream types build on them.

On this page
  1. What ostream is for
  2. What istream is for
  3. How the classes fit together
  4. Stream state
  5. Streams cannot be copied
  6. Common mistakes
  7. Summary

std::ostream and std::istream are the standard library classes for output and input. Objects such as std::cout are output streams, and std::cin is an input stream. The string stream classes in <sstream> are built on the same types, so the operators you learn with cout and cin also work on text in memory.

What ostream is for

An std::ostream is a stream you write to. The << operator sends values into it, and each << returns the stream itself, so you can chain them. This function takes any output stream by reference:

#include <iostream>
#include <sstream>
#include <string>

void print_pair(std::ostream& os, int a, int b) {
    os << "(" << a << ", " << b << ")";
}

int main() {
    print_pair(std::cout, 3, 4);
    std::cout << '\n';

    std::ostringstream buffer;
    print_pair(buffer, 5, 6);
    std::cout << buffer.str() << '\n';
}

The output is:

(3, 4)
(5, 6)

Because the function takes std::ostream&, it works with std::cout, with an std::ostringstream, and with a file stream.

What istream is for

An std::istream is a stream you read from. The >> operator pulls values out of it, and std::getline pulls whole lines.

#include <iostream>
#include <sstream>

int read_sum(std::istream& is) {
    int total = 0;
    int value = 0;
    while (is >> value) {
        total += value;
    }
    return total;
}

int main() {
    std::istringstream in("1 2 3 4");
    std::cout << read_sum(in) << '\n';  // 10
}

If you passed std::cin to read_sum instead, it would keep adding numbers until the input ends or a value that is not a number appears.

How the classes fit together

The classes form a small family tree:

  • std::ios holds the state that every stream shares.
  • std::ostream and std::istream both derive from std::ios.
  • std::iostream derives from both std::ostream and std::istream.
  • std::ostringstream and std::ofstream derive from std::ostream. std::istringstream and std::ifstream derive from std::istream. std::stringstream and std::fstream derive from std::iostream.

You do not need to memorize the tree. The useful point is that a function written for std::ostream& also accepts std::cout, a file stream, and an std::ostringstream.

Stream state

A stream tracks its condition with flags:

  • good() is true when no error flag is set.
  • eof() is true after the stream has reached the end of its input.
  • fail() is true after an operation failed, such as a read that found no valid number.
  • bad() is true after a more serious, unrecoverable error.

if (in) checks whether the stream is still usable. It is the same as checking that fail() is false. After a failure, the stream stays in that state until you call clear(), which is the same behavior you see with std::cin.

Streams cannot be copied

A stream holds state, so it cannot be copied. Pass one to a function by reference, using std::ostream& or std::istream&. Passing a stream by value does not compile.

Common mistakes

  • Passing a stream by value. Take it by reference instead, as shown above.
  • Using std::endl on every line. std::endl writes a newline and flushes the stream, which can slow down heavy output. '\n' writes only the newline, and it is usually the better choice for plain line breaks.
  • Taking the wrong direction. A function that takes std::istream& can read from the stream but not write to it. Use std::ostream& for output, or std::iostream& when it needs both.

Summary

std::ostream is for writing and std::istream is for reading. cout and cin are the familiar examples, and the string stream classes use the same types. Take streams by reference, and remember that clear() resets a stream after a failure.

The complete guide to stringstream

stringstream in C++
Read the guide

Keep exploring

Guides from other topics

All topics →