473,591 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary stream with std::stringstre am problem

Hi there,

I am using the std::stringstre am 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<dou blevector (2, 12.3456);
std::stringstre am stream
(std::stringstr eam::out|std::s tringstream::bi nary);

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 21192

akit...@gmail.c om napsal:
Hi there,

I am using the std::stringstre am 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<dou blevector (2, 12.3456);
std::stringstre am stream
(std::stringstr eam::out|std::s tringstream::bi nary);

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.c om napsal:
Hi there,

I am using the std::stringstre am 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<dou blevector (2, 12.3456);
std::stringstre am stream
(std::stringstr eam::out|std::s tringstream::bi nary);

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
interoperabilit y 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
36493
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
7482
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 = "value1" + " : " + "value2";
1
4892
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
12050
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 //----------------------------------------------------------------------------- #include <sstream> #include <iostream> using namespace std;
1
3135
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
4188
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
3593
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
7
3285
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 overload the SetValue method. .... protected:
3
5892
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 up into individual elements. The elements can be strings, integers or floating point numbers. In the object where I break the line into elements I use a std::stringstream object to do the actual check:
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7870
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8362
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8225
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6639
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
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 we have to send another system
1
1465
muto222
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.