L
Luther Baker
Hi,
My question is regarding std::istringstream. I am serializing data to
an ostringstream and the resulting buffer turns out just fine.
But, when I try the reverse, when the istringstream encounters the two
byte shorts, it either thinks it has reached the null terminator? or
eof and consequently stops reading values back in. It doesn't matter
whether or not I use the std::ios::binary flag when opening the
istringstream or the readsome method which does in-fact, not report
eof. It still can't build the shorts back up.
Any suggestions would be appreciated. Here is a brief example:
#include <cstring>
#include <sstream>
int
main(int argc, char** argv)
{
typedef unsigned short uint;
char* key = new char[8];
strncpy (key, "the key", 7)[7] = '\0';
uint var1 = 'a';
uint var2 = 'b';
std:stringstream os;
os.write(key, 8);
os.write(reinterpret_cast<const char*>(&var1), sizeof(var1));
os.write(reinterpret_cast<const char*>(&var2), sizeof(var2));
std::cout << "stream string: " << os.str() << std::endl;
std::cout << "stream c_str: " << os.str().c_str() << std::endl;
char* keytwo = new char[8];
keytwo[7] = 0;
uint feedme1 = 0;
uint feedme2 = 0;
//
// Here is where my question lies - how to initialize istringstream?
//
std::istringstream is (os.str().c_str(), std::ios::binary);
is.read(keytwo, 8);
std::cout << "\n\nkeytwo: " << keytwo << std::endl;
if (is.eof())
{
std::cout << "END OF FILE!!!" << std::endl;
}
is.read(reinterpret_cast<char*>(&feedme1), sizeof(feedme1));
std::cout << "feedme1: " << feedme1 << std::endl;
is.read(reinterpret_cast<char*>(&feedme2), sizeof(feedme2));
std::cout << "feedme2: " << feedme2 << std::endl;
//
// but it is clear the both shorts are present in the c_str
//
for (int i = 0; i < 12; ++i)
{
std::cout << os.str().c_str() << std::endl;
}
//
// and my favorite way to see what's happening
//
for (int i = 0; i < 12; ++i)
{
std::cout << (int)os.str().c_str() << std::endl;
}
delete [] key;
delete [] keytwo;
return 0;
}
Thanks,
-Luther
My question is regarding std::istringstream. I am serializing data to
an ostringstream and the resulting buffer turns out just fine.
But, when I try the reverse, when the istringstream encounters the two
byte shorts, it either thinks it has reached the null terminator? or
eof and consequently stops reading values back in. It doesn't matter
whether or not I use the std::ios::binary flag when opening the
istringstream or the readsome method which does in-fact, not report
eof. It still can't build the shorts back up.
Any suggestions would be appreciated. Here is a brief example:
#include <cstring>
#include <sstream>
int
main(int argc, char** argv)
{
typedef unsigned short uint;
char* key = new char[8];
strncpy (key, "the key", 7)[7] = '\0';
uint var1 = 'a';
uint var2 = 'b';
std:stringstream os;
os.write(key, 8);
os.write(reinterpret_cast<const char*>(&var1), sizeof(var1));
os.write(reinterpret_cast<const char*>(&var2), sizeof(var2));
std::cout << "stream string: " << os.str() << std::endl;
std::cout << "stream c_str: " << os.str().c_str() << std::endl;
char* keytwo = new char[8];
keytwo[7] = 0;
uint feedme1 = 0;
uint feedme2 = 0;
//
// Here is where my question lies - how to initialize istringstream?
//
std::istringstream is (os.str().c_str(), std::ios::binary);
is.read(keytwo, 8);
std::cout << "\n\nkeytwo: " << keytwo << std::endl;
if (is.eof())
{
std::cout << "END OF FILE!!!" << std::endl;
}
is.read(reinterpret_cast<char*>(&feedme1), sizeof(feedme1));
std::cout << "feedme1: " << feedme1 << std::endl;
is.read(reinterpret_cast<char*>(&feedme2), sizeof(feedme2));
std::cout << "feedme2: " << feedme2 << std::endl;
//
// but it is clear the both shorts are present in the c_str
//
for (int i = 0; i < 12; ++i)
{
std::cout << os.str().c_str() << std::endl;
}
//
// and my favorite way to see what's happening
//
for (int i = 0; i < 12; ++i)
{
std::cout << (int)os.str().c_str() << std::endl;
}
delete [] key;
delete [] keytwo;
return 0;
}
Thanks,
-Luther