Questions tagged [ostringstream]

Output stream class to operate on strings.

150 questions
6
votes
3 answers

Why does ostringstream strip NULL?

I have a string whose last part(suffix) needs to be changed several times and I need to generate new strings. I am trying to use ostringstream to do this as I think, using streams will be faster than string concatenations. But when the previous…
balki
  • 26,394
  • 30
  • 105
  • 151
6
votes
2 answers

Convert array of uint8_t to string in C++

I have an array of type uint8_t. I want to create a string that concatenates each element of the array. Here is my attempt using an ostringstream, but the string seems to be empty afterward. std::string key = ""; std::ostringstream convert; for (int…
Alex Parker
  • 1,533
  • 3
  • 16
  • 38
6
votes
2 answers

Is it possible to change ostringstream rdbuf?

I try the following code: ostringstream oss; streambuf *psbuf, *backup; backup = oss.rdbuf(); psbuf = cout.rdbuf(); oss.rdbuf(psbuf); oss << things << endl; oss.rdbuf(backup); But unfortunately I get this error: error: too…
user3391320
  • 1,023
  • 7
  • 9
6
votes
1 answer

std::ostringstream isn't returning a valid string

I'm trying to use std::ostringstream to convert a number into a string (char *), but it doesn't seem to be working. Here's the code I have: #include #include int main() { std::ostringstream out; out << 1234; const…
AutoBotAM
  • 1,485
  • 3
  • 18
  • 24
5
votes
3 answers

How to write a `<<` operator for boost::tuple?

In the sample code below, it shows that boost::tuple can be created implicitly from the first template argument. Because of that I am not able to write a << operator as it becomes ambiguous. Also I don't understand why ostringstream& << float is…
balki
  • 26,394
  • 30
  • 105
  • 151
5
votes
2 answers

CRLF end of line and ostringstream

I am trying to use a ostringstream to build a string which uses the platform standard line endings (so for me it is CRLF since I am developing for Windows). I tried the following code: std::ostringstream out; out << "line1\nline2\n"; const char*…
JMD
  • 218
  • 1
  • 2
  • 6
5
votes
1 answer

C++ std::stringstream/ostringstream and UTF characters

I'm writing a program which processes some data, outputs it to a .csv file, then writes a GNUplot script, and calls GNUplot to execute the script and create an image file all with the same name (only different extensions). The filenames contain UTF…
pavichokche
  • 91
  • 1
  • 8
5
votes
1 answer

Reading binary data in memory into a stringstream and stream this over a socket

I would like to know if it is possible to, for instance, take a piece of data in memory, read it into an output stringstream (as binary data) and write this onto a socket for a client application to process. The problem I run into while attempting…
Jan Swart
  • 6,761
  • 10
  • 36
  • 45
4
votes
1 answer

C++ Int to String by using ostringstream or stringstream

I've been using stringstream to convert Integer to String, but then I realized same operation can be done with ostringstream. When I use .str() what is the difference between them? Also, is there more efficient way to convert integers to…
Malkavian
  • 372
  • 1
  • 5
  • 16
4
votes
3 answers

How would std::ostringstream convert to bool?

I stumbled across this code. std::ostringstream str; /// (some usage) assert( ! str ); What does ostringstream signify when used in a bool context? Is this possibly an incorrect usage that happens to compile and run?
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
4
votes
2 answers

Odd behavior with ostringstream

I was trying to think of a clever way to concatenate various things into a single string argument for a function without having to use an ostringstream explicitly. I thought of: #define OSS(...) \ dynamic_cast
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
4
votes
3 answers

How to use an ostringstream in just an expression

I use std::ostringstream for formatting strings, which inherits its << operators from ostream, and consequently they return an ostream and not an ostringstream, which means that I can't call ostringstream::str on the result. This usually isn't a…
Anonymous
  • 491
  • 2
  • 12
4
votes
2 answers

How to stream float .1 as .1 and not 0.1

std::ostringstream oss; oss << std::setw(10); oss << std::setfill(' '); oss << std::setprecision(3); float value = .1; oss << value I can check if value < 1 and then find the leading zero and remove it. Not very elegant.
John
  • 541
  • 4
  • 13
4
votes
5 answers

How to convert a char array into string based hex-stream (ostringstream)

in C++ (on Linux with gcc) I'd like to put a byte array (vector) to a ostringstream or a string. I know that I can use sprintf but it doesn't seem to be the best way to use char* also. btw: this link did not help Edit: All answer work…
tuergeist
  • 9,171
  • 3
  • 37
  • 58
4
votes
4 answers

C++ ostringstream strange behavior

I had a very strange problem with c++ code recently. I have reproduced the case in minimalist example. We have an Egg class: class Egg { private: const char* name; public: Egg() {}; Egg(const char* name) { this->name=name; } …
Borislav Stoilov
  • 3,247
  • 2
  • 21
  • 46
1
2
3
9 10