Function fprintf writes to FILE*. I have a debugPrint function which writes to stringstream. I don't want to change my function for it is used at many places and it would break the code.
How can I pass a stringstream to fprintf?
UPDATE : Can I wrap the stringstream to a FILE and unwrap back?
You can't "pass" the ostream to fprintf. The FILE structure is platform dependent.
This is probably not the best way, but you can however create an evil macro:
#undef fprintf
#define fprintf my_fprintf
And you can create two my_fprintfs, one that takes FILE*, and another that takes std::stringstream& as argument.
You can avoid the macro by simply calling my_fprintf directly, but you have to modify the call site.
You can't "wrap stringstream in FILE*" portably, but there are some system specific ways. If that's what you after, then Linux for example has fopencookie() which take function pointers to read/write functions. You could then create functions that wrap std::stringstream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With