Concepts / Working with String Streams

Working with String Streams

A string stream is a stream that works with a string instead of a keyboard, a file, or the screen.

  • Programming

Why a String Needs a Stream

A string stream is a stream that works with a string instead of a keyboard, a file, or the screen. Most of the time you have already met streams in the form of input from a keyboard or output to the screen. A string stream applies the same stream idea to text that already lives inside your program.

Before reading on, think about this situation: a line of text contains several numbers separated by spaces, and your program needs each number as a separate value. Which direction does the data move, from the text into your program or from your program into the text? Keep your answer in mind, because the rest of this article is about matching that direction to the right tool.

Following the Direction of Data

Each string stream type is named for the direction in which data moves. Think of each stream as a channel with a direction. An istringstream carries data out of a string toward your program. An ostringstream carries data from your program into a string. A stringstream is a channel that can carry data in either direction, depending on which operations you use.

stringprogramstringistringstreamostringstreamstringstreamprogramstringprogramread fromvalues outvalues inassembled textread fromvalues outvalues inwrite to
Which way does data move for each type: from the string into the program for reading, from the program into the string for writing, or both ways for stringstream?

Read the diagram from left to right in each row. The top row shows the reading direction: data moves from the string into the program through an istringstream. The middle row shows the writing direction: data moves from the program into a string through an ostringstream. The bottom row shows the stringstream, which is not limited to either direction. The same object can take part in either flow, because it supports both reading from and writing to a string.

The Three Stream Types

The three types are closely related. Each one is a stream attached to a string, and the only difference between them is which operations they support. Learning them as one family, with one small difference between members, is easier than memorizing three unrelated tools.

Declaring Each Stream Type

Each type is declared with its name followed by the name you choose for the stream object. In the declarations below, s is that chosen name. The code is in C++, since the string streams and std::string belong to that language. Each line is shown on its own because all three use the same name s, so they would conflict if placed in the same scope.

cpp
Notes on the code
  • istringstream s declares a stream for reading from a string
  • ostringstream s declares a stream for writing to a string
  • stringstream s declares a stream for reading from and writing to a string

Comparing the Three Types Side by Side

Placing the three types next to each other makes the single point of difference clear. Each type is a string stream. What changes is the direction it supports.

istringstreamreads from a stringostringstreamwrites to a stringstringstreamreads and writes
How do istringstream, ostringstream, and stringstream differ in their capabilities, and which one matches a given task?
Stream typeDirection it supportsName signalsWhen to choose it
istringstreamReading from a stringInputYou need values taken out of a string
ostringstreamWriting to a stringOutputYou need to assemble a string from values
stringstreamReading from and writing to a stringBothThe task needs data to move in both directions

Choosing among the three string stream types

Choosing a Stream for a Task

Worked example

Matching a task to a stream type

A program has the text 3 4 5 stored in a string and needs the three numbers as separate values. Then it must build a new sentence from those values and store it in a string. Which stream types fit each part?

  1. 1
    Identify the first direction

    Taking numbers out of the text 3 4 5 means data moves from the string into the program. That is reading, so the matching type is istringstream.

  2. 2
    Identify the second direction

    Building a new sentence means data moves from the program into a string. That is writing, so the matching type is ostringstream.

  3. 3
    Check whether one stream must do both

    If a single stream object must both read the original text and write the new text, the task needs both directions, and stringstream is the general-purpose choice. If the two parts are separate jobs, two single-direction streams are the more direct match.

Result

The number extraction uses istringstream, the sentence building uses ostringstream, and a single stringstream would be chosen only when one object must carry data in both directions.

A Stream Attached to a String

Each string stream is attached to a string. The stream does not replace the string. It works on the text that the string holds, and your program interacts with that text through the stream. The diagram below shows which object contains which, and where your program fits.

programstring streamistringstream,ostringstream, orstringstreamunderlying stringstd::stringreads or writes throughattached to
How does a string stream object relate to the underlying std::string it reads from or writes to, and what contains what?

Mistakes When Picking a Stream Type

  • Common mistake

    Choosing a stream type by its familiarity instead of by the direction the data moves

    What it looks like

    Using ostringstream to pull numbers out of a line of text because output streams feel more familiar.

    Why it fails

    The name signals the direction. ostringstream is for writing to a string, so it cannot serve a reading task.

    Do this instead

    Ask first whether data flows from the string into the program or from the program into the string, then pick the type whose name matches that direction.

  • Common mistake

    Using stringstream for every task out of habit

    What it looks like

    Declaring a stringstream to only read a line of text when the task never writes anything.

    Why it fails

    stringstream supports both directions, but a task that only reads does not need that extra capability, and the choice no longer signals the actual purpose of the code.

    Do this instead

    Use istringstream when the task only reads and ostringstream when it only writes. Reserve stringstream for tasks that genuinely need both directions.

  • Common mistake

    Thinking the string stream replaces the string

    Why it fails

    Each string stream is attached to a string. The stream works on the text the string holds, so the string is still the thing the data lives in.

    Do this instead

    Keep track of which string the stream is attached to, since the data you read or write is the content of that string.

Check Your Choice

EASY

A program receives a single line of text that contains a name followed by a score, and it must extract both values. Which string stream type should it use, and what direction is the data moving? Explain your choice in one or two sentences.

Hints
  • Ask whether the data starts in the string or in the program.
  • Look for the type whose name signals the direction of this task.
  • Only choose stringstream if the same object must also write back into the string.

Key Takeaways

  • A string stream is a stream that works with a string instead of a keyboard, a file, or the screen.
  • istringstream is used for reading from a string, ostringstream is used for writing to a string, and stringstream is used for both.
  • The three types are one family that differ only in which operations they support, and each type's name signals its direction.
  • To choose a type, identify whether data moves from the string into the program, from the program into the string, or in both directions.

Related concepts

More concepts starting with W