Redirecting C++ ostreams

Jeremy DeHaan dehaan.jeremiah at gmail.com
Thu Jun 13 21:13:14 PDT 2013


Not sure if anyone cares too much since I doubt this will come up 
very often, but on the off chance it does I came up with a 
solution that will at least work for me.

In my C/C++ code I created two variables; std::ostringstream 
outputStream and std::string outputString. outputStream is what I 
used to redirect the underlying ostream, and then I made a 
function as follows:

const char* getOutput()
{
     outputString = outputStream.str();

     outputStream.str("");

     return outputString.c_str();
}

And then in my D code(after linking it to my C code) I do one of 
these after I know something was written to the ostream:

stderr.write(text(getOutput()));


This way I can redirect stderr wherever I want and still send the 
contents of the ostream to the same place.





More information about the Digitalmars-d-learn mailing list