469,926 Members | 2,533 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

binary stream with std::stringstream problem

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
2 18593

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

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.

Similar topics

6 posts views Thread by Giampiero Gabbiani | last post: by
2 posts views Thread by Woodster | last post: by
1 post views Thread by KidLogik | last post: by
5 posts views Thread by Mr Fish | last post: by
1 post views Thread by kathy | last post: by
5 posts views Thread by Marcin Kalicinski | last post: by
7 posts views Thread by TBass | last post: by
3 posts views Thread by Rune Allnor | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.