0

I have a c++ library that provides an object with complicated logic. So far, many objects print their output to std::cout when one single library command is executed. I want to create a Qt GUI for this (output it in a text widget instead of console output), so std::cout has to be changed for something more flexible. I have passed a reference to std::stringstream parameter to the main object (and it passes it down to lower objects) and replaced all "std::cout << " with "stream << " (stream is std::stringstream).

It doesn't work, because I lose most of the output. When I want to fetch it from the stringstream, it's ridiculously small (just few characters). It seems that if I do stream << var1 << var2 << var3 only var3 will be available.

Don't know how to solve this problem. If I passed a reference to std::cout as the main object parameter, everything would be ok under console, but it'd not work for the GUI application (I guess I cannot fetch output from std::cout).

Please tell me if I'm using streamstring in a bad way. Or let me know if there is a better way to fetch the output (use something else instead of stringstream). Many thanks in advance.

ducin
  • 25,621
  • 41
  • 157
  • 256
  • At some point in time look carefully and see if a local-scope stream (like a local `ostrstream` with the *same name* as the passed-down parameter) hides the parameter and eats (and throws away on scope-exit) all of your insertions. By the sound of it, it would be fairly early in the chain of passing. – WhozCraig Jan 27 '13 at 02:00
  • Should be fine, see https://ideone.com/ZSCsSc - But, as for logging events and stuff, I would much, much prefer a concurrent (or non-concurrent if you don't need it) queue. – cooky451 Jan 27 '13 at 02:03
  • 2
    @cooky451 any reason that parameter in the ideone snippet posted is a `std::stringstream&` reference? why not just a generic `std::ostream&`, thereby supporting files, console, etc. ? – WhozCraig Jan 27 '13 at 02:06
  • If you decide to go the simpler route, you can leave the existing code intact, and use `popen` (or `_popen`) to spawn it and catch what it writes to `stdout`, then display that as you see fit. – Jerry Coffin Jan 27 '13 at 02:16
  • 2
    Without a repro case, we cannot help you! Please read entirely [Short, Self Contained, Correct (Compilable), Example](http://sscce.org/) Copy your whole project to a new directory and keep deleting project files until this specific problem can be demonstrated in a page of code. You'll probably spot the problem yourself in the process... – HostileFork says dont trust SE Jan 27 '13 at 02:27

1 Answers1

0

you could use freopen("out.txt","w",stdout); at the start of your program.

elios264
  • 385
  • 2
  • 16
  • 1
    or also http://stackoverflow.com/questions/191842/how-do-i-get-console-output-in-c-with-a-windows-program#answer-191912 – elios264 Jan 27 '13 at 03:34