By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,409 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,409 IT Pros & Developers. It's quick & easy.

binary stream with std::stringstream problem

P: n/a
Hi there,

I am using the std::stringstream to create a byte array in memory. But
it is not working properly. Can anyone help me?

Code:

#include <vector>
#include <sstream>
#include <iostream.h>

int main(int argc, char* argv[])
{
std::vector<doublevector (2, 12.3456);
std::stringstream stream
(std::stringstream::out|std::stringstream::binary) ;

unsigned int sz = vector.size();
stream << sz;
for (unsigned int i = 0; i < sz; i++)
{
stream << vector[i];
}

//results
std::string buffer = stream.str();
cout << buffer.c_str() << endl;

return 0;
}

output:
212.345612.3456

Why is the output not some byte-sequence, but formatted text?

Dec 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

akit...@gmail.com napsal:
Hi there,

I am using the std::stringstream to create a byte array in memory. But
it is not working properly. Can anyone help me?

Code:

#include <vector>
#include <sstream>
#include <iostream.h>

int main(int argc, char* argv[])
{
std::vector<doublevector (2, 12.3456);
std::stringstream stream
(std::stringstream::out|std::stringstream::binary) ;

unsigned int sz = vector.size();
stream << sz;
for (unsigned int i = 0; i < sz; i++)
{
stream << vector[i];
}

//results
std::string buffer = stream.str();
cout << buffer.c_str() << endl;

return 0;
}

output:
212.345612.3456

Why is the output not some byte-sequence, but formatted text?
Which output do you expect? it is correct, because first you are
writing to output length of vector (which is 2) and then you dump all
vector items without delimiters (both items are 12.3456).

Btw. your source is not correct, there is used #include <iostream.h>
(but should be <iostream>) and there are missing some std:: prefixes.

Dec 14 '06 #2

P: n/a

ak*****@gmail.com napsal:
Hi there,

I am using the std::stringstream to create a byte array in memory. But
it is not working properly. Can anyone help me?

Code:

#include <vector>
#include <sstream>
#include <iostream.h>

int main(int argc, char* argv[])
{
std::vector<doublevector (2, 12.3456);
std::stringstream stream
(std::stringstream::out|std::stringstream::binary) ;

unsigned int sz = vector.size();
stream << sz;
for (unsigned int i = 0; i < sz; i++)
{
stream << vector[i];
}

//results
std::string buffer = stream.str();
cout << buffer.c_str() << endl;

return 0;
}

output:
212.345612.3456

Why is the output not some byte-sequence, but formatted text?
I see, you want binary output. You cannot use operator<< for binary
output. Use method 'write' of stream.

Btw, in 99% is writing binary output not good idea (for
interoperability and portability reasons).

Dec 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.