Possible Duplicate:
How to force std::stringstream operator >> to read an entire string?
I am trying to convert a structure to a string, similar to how toString() would work for an object in Java or C++. To do this I am writing my formatted data to a std::stringstream then write that to a std::string.
Here is what I have:
std::stringstream ss;
std::string packet;
ss << "Packet Length: " << p->header->len
<< " (" << p->header->caplen << ")" << std::endl
<< "Collected: " << timedate << "."
<< std::dec << p->header->ts_usecs << std::endl
<< "Eth:\tFrom: " << to_address(p->from) << std::endl
<< "\tTo: " << to_address(p->to) << std::endl
<< "\tType: " << to_hex(p->type, false)
<< " (" << p->type_name << ")" << std::endl;
But for some reason when I write this stream into the std::string packet:
ss >> packet;
and then print the value of packet:
cout << "Packet X " << packet << endl;
I only see the text "Packet" and nothing else.
Is there something obvious I am missing here?
Instead of reading a string back, you should get the value using the
stringstream ::strmember:Otherwise, the reading follows the usual pattern of treating spaces as separators.