String Streams

The stringstream is a class that is useful for extracting data from or writing formatted data to strings.

A very common question, for example is this: ``How do I convert a string to a number ? ''. Of course, there's no way to do so in general, since not all strings look like numbers. But it's certainly not unusual that we'd want to extract a number from a string. Note that the g++ compiler ships without the sstream header. You can download it from here Here's how one does it:

stringstream1.cc

FAQ: How Do I Convert String To Number ?


#include <iostream>
#include <sstream>
using namespace std;
int main()
{
	int a;
	string s = "456";
	istringstream sin(s);
	sin >> a;
	cout << a << endl;
	return 0;
}

The istringstream class is an input stream attached to a string. The constructor copies the string s into its private buffer, and then we may extract data from it using regular stream semantics, in this case we use sin>>a.

FAQ: How Do I Convert A Number To A String ?

or how do I do ``sprintf'' in C++ ?

You may have guessed -- the answer is to use an ostringstream. This data type behaves in a similar way to the istringstream. The main difference is that there is an extra method, str(). This method returns the string that lies in the ostringstream object. There's also a verision of str() that takes a string argument -- this initialises the streams underlying string buffer to that argument. This is commonly used to clear a stream for reuse -- one can call mystream.str("");

stringstream4.cc


#include <sstream>
#include <iostream>
int main()
{
    std::ostringstream strout;
    int x = 42;
    strout << "The answer to the question is " << 42 << std::endl;
    cout << strout.str() << endl;
    strout.str("");
    strout << 53.2;
    cout << strout.str() << endl;
    return 0;
}

Using Stringstreams To Parse Input

A problem that often comes up is this: suppose you have written the following code:

stringstream2.cc


#include <iostream>
	
using namespace std;
int main()
{
    int x;
    do
        cout << "Enter a positive integer (0 to quit)" << endl;
    while ( cin >> x && x != 0 );
    return 0;
}

What happens if the user enters

2 3
Or worse, if they enter:
5.0
The problem is that the extraction operator does not expect each item extracted to be on a seperate line. So if you do expect this, you need to be explicit about it in your code. The way to do this is use getline() to read a line of input into a string and then use that string to create an istringstream from which we can extract data. After we've extracted the data we need, we can check for trailing garbage. Here's an example:

stringstream3.cc


#include <sstream>
#include <iostream>

using std::cin;
using std::cout;
using std::end;

int main()
{
    int x;
    char ch;
    std::string myString;
    while (getline ( cin, myString ))
    {
        std::istringstream strin(myString);
        strin >> x;
        if (!strin)
            cout << "Bad input" << endl;
        else if ( strin >> ch )
            cout << "Bad input" << endl;
        else
            cout << "You entered: " << x << endl;
    }
    return 0;
}


Some notes:

strstream considered harmful

There exists a deprecated class similar to stringstream that is called strstream. Do not confuse these two classes. They are not the same. strstream has a very error prone interface because of the way it handles memory. One problem is that it does not append trailing nulls when str() is called. So you must append a trailing null, or std::ends. Another important thing to remember is that if you use ostrstream with a dynamic buffer, like this:

std::ostrstream strout;
strout << "The answer is ... " << 42 << std::endl << std::ends;
strout.str();
Then calling str has the peculiar side effect that the caller is responsible for managing the memory allocated by strout's buffer, which is pretty silly since the caller does not know how the memory was allocated (it may be allocated using malloc() or new). So to make the stupid thing take its memory back, one makes the following call:
strout.freeze(0);

It's worth mentioning that there is another, safer way to use ostrstream and that is to use it with a static buffer. If you do this, you don't need to deal with this freeze() nonsense. To do this, call the constructor that takes a character array as an argument.

char a[100];
std::ostrstream strout(a,100);
strout << "the answer is" << 42 << std::endl << std::ends;
std::cout << a << std::endl;