ostringstream

C++ format macro / inline ostringstream

I'm trying to write a macro that would allow me to do something like: FORMAT(a << "b" << c << d), and the result would be a string -- the same as creating an ostringstream, inserting a...d, and returning .str(). Something like: string f(){ ostringstream o; o << a << "b" << c << d; return o.str() } Essentially, FORMAT(a << "b" ...

Inline ostringstream macro reloaded

Referring to http://stackoverflow.com/questions/303562/c-format-macro-inline-ostringstream The question there was for a macro that allows inline concatenation of objects to create a string, iostream-style. The answer was: #define SSTR( x ) dynamic_cast< std::ostringstream & >( \ ( std::ostringstream().seekp( 0, std::ios_base::...

How to reuse an ostringstream?

I'd like to clear out and reuse an ostringstream (and the underlying buffer) so that my app doesn't have to do as many allocations. How do I reset the object to its initial state? ...

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? ...

Mac OS X port crashes in pthread_setspecific in glibstdc++ vsnprintf - how to troubleshoot?

I'm testing a Mac OS X port of my multithreaded server. It starts up, but it dies in vsnprintf soon after the first client request is taken by a worker thread. It seems that vsnprintf is trying to manipulate some thread local memory with pthread_setspecific. This dereferences a bad pointer. Then, gdb traps a dlopen call, gets an error,...

Dynamic output filenames (C++)

I'm trying to create output files subscripted by a dynamic index ( d = {0,...,NUM_DEMES-1}). Currently, I'm only getting output files for the first value (d=0). #include <sstream> #include <string> void Simulation::updateSimulation( double t ) { ... ofstream abundanceStream; ofstream abHeaderStream; if ( step == 1 ) { for ...

How to hook up Boost serialization & iostreams to serialize & gzip an object to string?

Hi all; I've been using the Boost serialization library, which is actually pretty nice, and lets me make simple wrappers to save my serializable objects to strings, like so: template <class T> inline std::string saveString(const T & o) { std::ostringstream oss; boost::archive::binary_oarchive oa(oss); oa << o; return oss.str(); } te...

Is there anyway to write the following as a C++ macro?

my_macro << 1 << "hello world" << blah->getValue() << std::endl; should expand into: std::ostringstream oss; oss << 1 << "hello world" << blah->getValue() << std::endl; ThreadSafeLogging(oss.str()); Thanks! EDIT: the accepted answer is awesome. Can we upvote 8 more times and win this responder a badge? (The answer only needs 6 mor...

What use is there for 'ends' these days?

I came across a subtle bug a couple of days ago where the code looked something like this: ostringstream ss; int anInt( 7 ); ss << anInt << "HABITS"; ss << ends; string theWholeLot = ss.str(); The problem was that the ends was sticking a '\0' into the ostringstream so theWholeLot actually looked like "7HABITS\0" (i.e. a null at the e...

Is there a way to reduce ostringstream malloc/free's?

I am writing an embedded app. In some places, I use std::ostringstream a lot, since it is very convenient for my purposes. However, I just discovered that the performance hit is extreme since adding data to the stream results in a lot of calls to malloc and free. Is there any way to avoid it? My first thought was making the ostringstrea...

Converting a char array to something that can be appended to a ostringstream

std::ostringstream parmStream; char parmName[1024]; THTTPUrl::Encode(parmName, pParm->Name().c_str(), 1024); //I want to add the value of the paramName to the parmStream worked b4 when parmName was a string but obv not now parmStream << "&" << parmName + "="; Gives me the following .. error: invalid operands of types \u2018char ...

Deriving streambuf or basic_ostringstream?

Hi, I want to derive a stringstream so that I can use the operator<< to construct a message which will then be thrown. The API would look like: error("some text") << " more text " << 42 << std::endl; This should do a throw "some text more text 42" So what I did is make an errorbuf (inheriting from streambuf) which overloads the 'ov...

Truncate C++ string fields generated by ostringstream, iomanip:setw

In C++ I need string representations of integers with leading zeroes, where the representation has 8 digits and no more than 8 digits, truncating digits on the right side if necessary. I thought I could do this using just ostringstream and iomanip.setw(), like this: int num_1 = 3000; ostringstream out_target; out_target << setw(8) << ...

C++ STL in VS2008: std::ostringstream throws std::bad_alloc after heavy assign/clear usage

Hi all, I have come across a situation (on Win32) where the std::ostringstream object continues to consume process memory, even when it is ostensibly cleared out after a series of append-type operations. Please take a look at this C++ fragment: int main(void) { std::ostringstream cOutputLogStream; // Random long string std...

What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

Hello, I think the title says pretty much everything, hehe. I would like you to tell me when to use std::istringstream, std::ostringstream and std::stringstream and why I shouldn't just use std::stringstream in every scenario (like are there any runtime performance issues?). Thanks! PS: I have a third, less important question. Is ther...

Rounding off floats with ostringstream

Hi, I have an issue regarding conversion from float to c++ string using ostringstream. Here is my line: void doSomething(float t) { ostringstream stream; stream << t; cout << stream.str(); } when t has value -0.89999 it is round off to -0.9, but when it's value is 0.0999 or lesser than this say 1.754e-7, it just prints w...

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

Hi, in C++ (on Linux with gcc) I'd like to put a byte array (vector<unsigned char>) 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 so far. But I did not meantion, that I'd like to convert the bytes/hex-values int...

What is the purpose of ostringstream's string constructor?

On MSVC 2005, I have the following code. std::ostringstream stream("initial string "); stream << 5; std::cout << stream.str(); What I expect is: initial string 5 What I get is: 5nitial string Initializing the stream with a string, I would expect the stream to move its position to the end of the initial string. Obviously, STL doe...