Check how many characters are stored in ostrstream
#include <strstream>
#include <iostream>
using namespace std;
main()
{
char str[80];
ostrstream outs(str, sizeof(str));
outs << "Hello ";
outs << 34 << " " << 1234.23;
outs << ends;
cout << outs.pcount(); // display how many chars in outs
cout << " " << str;
return 0;
}
Display how many chars in ostrstream
#include <strstream>
#include <iostream>
using namespace std;
int main()
{
char str[80];
ostrstream outs(str, sizeof(str));
outs << "abcdefg ";
outs << 27 << " " << 890.23;
outs << ends; // null terminate
cout << outs.pcount(); // display how many chars in outs
cout << " " << str;
return 0;
}
18 abcdefg 27 890.23"
Freeze dynamic buffer and return pointer to it
#include <strstream>
#include <iostream>
using namespace std;
int main()
{
char *p;
ostrstream outs;
outs << "AAAAAAAAAAAAAAAAAAAA";
outs << -10 << hex << " ";
outs.setf(ios::showbase);
outs << 100 << ends;
p = outs.str();
cout << p;
return 0;
}
AAAAAAAAAAAAAAAAAAAA-10 0x64"
Make ostringstream work with decimal, hexadecimal value, floating value, bitset
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <sstream>
#include <bitset>
using namespace std;
int main()
{
ostringstream os;
// decimal and hexadecimal value
os << "dec: " << 15 << hex << " hex: " << 15 << endl;
cout << os.str() << endl;
// append floating value and bitset
bitset<15> b(5789);
os << "float: " << 4.67 << " bitset: " << b << endl;
// overwrite with octal value
os.seekp(0);
os << "oct: " << oct << 15;
cout << os.str() << endl;
}
dec: 15 hex: f
oct: 17 hex: f
float: 4.67 bitset: 001011010011101
ostrstream with format
#include <strstream>
#include <iostream>
using namespace std;
int main()
{
char str[80];
ostrstream outs(str, sizeof(str));
outs << "AAAAAAAAAAAAAAAA. ";
outs << 1024 << hex << " ";
outs.setf(ios::showbase);
outs << 100 << " " << 99.789 << ends;
cout << str; // display string on console
return 0;
}
AAAAAAAAAAAAAAAA. 1024 0x64 99.789"
Output of several data types to an ostringstream object: double, int and address of int
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <sstream>
using std::ostringstream;
int main()
{
ostringstream outputString;
string string1( "string " );
double double1 = 123.4567;
int integer = 22;
outputString << string1 << double1 << integer << &integer;
// call str to obtain string contents of the ostringstream
cout << "outputString contains:\n" << outputString.str();
return 0;
}
outputString contains:
string 123.457220x22fe74"
Save string with ostringstream
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char** argv)
{
cout << "Enter tokens. Control-D (Unix) or Control-Z (Windows) to end." << endl;
ostringstream outStream;
while (cin) {
string nextToken;
cout << "Next token: ";
cin >> nextToken;
if (nextToken == "done") break;
outStream << nextToken << "\t";
}
cout << "The end result is: " << outStream.str();
}