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.
FAQ: How Do I Convert String To Number ?
1 #include <iostream>
2 #include <sstream>
3 using namespace std;
4 int main()
5 {
6 int a;
7 string s = "456";
8 istringstream sin(s);
9 sin >> a;
10 cout << a << endl;
11 return 0;
12 }
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("");
1 #include <sstream>
2 #include <iostream>
3 int main()
4 {
5 std::ostringstream strout;
6 int x = 42;
7 strout << "The answer to the question is " << 42 << std::endl;
8 cout << strout.str() << endl;
9 strout.str("");
10 strout << 53.2;
11 cout << strout.str() << endl;
12 return 0;
13 }
Using Stringstreams To Parse Input
A problem that often comes up is this: suppose you have written the following code:
1 #include <iostream>
2
3 using namespace std;
4 int main()
5 {
6 int x;
7 do
8 cout << "Enter a positive integer (0 to quit)" << endl;
9 while ( cin >> x && x != 0 );
10 return 0;
11 }
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:
1 #include <sstream>
2 #include <iostream>
3
4 using std::cin;
5 using std::cout;
6 using std::end;
7
8 int main()
9 {
10 int x;
11 char ch;
12 std::string myString;
13 while (getline ( cin, myString ))
14 {
15 std::istringstream strin(myString);
16 strin >> x;
17 if (!strin)
18 cout << "Bad input" << endl;
19 else if ( strin >> ch )
20 cout << "Bad input" << endl;
21 else
22 cout << "You entered: " << x << endl;
23 }
24 return 0;
25 }
Some notes:
* The istringstream object is declared within the loop, so its scope is the block of the loop. So a new istringstream object is created and the old one is destroyed for each iteration of the loop.
* The test (!strin) checks to see if the stream is in an error state.
* The attempt to extract a character is a test to see if there's anything left in the stream after we extract an integer. Note that this ignores whitespace (which is the desired effect in this case)
* If there are no problems, then the extraction was succesful.
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;