binary stream with std::stringstream problem

A

akitoto

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<double> vector (2, 12.3456);
std::stringstream stream
(std::stringstream::eek:ut|std::stringstream::binary);

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

//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?
 
O

Ondra Holub

(e-mail address removed) 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<double> vector (2, 12.3456);
std::stringstream stream
(std::stringstream::eek:ut|std::stringstream::binary);

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

//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.
 
O

Ondra Holub

(e-mail address removed) 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<double> vector (2, 12.3456);
std::stringstream stream
(std::stringstream::eek:ut|std::stringstream::binary);

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

//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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top