472,111 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

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 20163

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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.