How are you determining that the content is truncated? A stringstream
can contain null characters and std::string s = ss.str()
will copy those null characters and the characters after it to the std::string
object.
However, if you then use functions that treat the std::string
object's contents as a C style string, it will appear truncated:
#include <sstream>
#include <string>
#include <iostream>
#include <ostream>
#include <string.h>
using namespace std;
stringstream ss;
int main()
{
ss << 'a' << 'b' << 'c' << (char) '\0' << '1' << '2' << '3';
string s = ss.str();
cout << s.size() << endl;
cout << s.c_str() << " C string length: " << strlen(s.c_str()) << endl;
cout << s << endl;
}
produces the following output:
7
abc C string length: 3
abc 123