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? 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. 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). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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";
|
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);
|
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;
|
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 ???
| |
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
|
by: ma740988 |
last post by:
Consider:
#include <iostream>
#include <sstream>
#include <string>
int main ( ) {
{
double pi = 3.141592653589793238;
std::stringstream a;
|
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:
|
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:
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |