473,473 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 21139

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Giampiero Gabbiani | last post by:
Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success... Thanks in advance Giampiero
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
5
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy ...
1
by: kathy | last post by:
I am trying to convert integer to std::string by using: int m,n; std::string s1,s2; std::stringstream sStream; sStream << m; s1 = sStream.str(); ????? how to clear up sStream ???
5
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
7
by: TBass | last post by:
Hi. I wrote a "tag" class to store values. The user gets to store most any type he would need. Instead of getting too complicatated, I decided I would store the value as a stringstream, then...
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.