1

I have two GUI apps and one of them runs the other using QProcess. The second application does some work and writes a resulting string to standard out. The first application is waiting for a readyReadStandardOutput signal to be emitted from the QProcess. The problem is I don't receive this signal while the second app is running - I have to close it to obtain resulting string. I tried the same things with a simple console application and everything was fine. Can anyone help?

nobody
  • 19,814
  • 17
  • 56
  • 77
Mikhail Zimka
  • 694
  • 1
  • 9
  • 20
  • Are you running the second application as W32 or console application? In my experince cin/cout does not cope well with a non-console application. [This question](http://stackoverflow.com/questions/191842/how-do-i-get-console-output-in-c-with-a-windows-program) might be of interest to you then. – MatthiasB May 20 '14 at 12:31
  • Yes, I'm running win32 application. But as as @hyde said, the problem was just in stdout buffering. – Mikhail Zimka May 21 '14 at 07:33

1 Answers1

2

Problem is probably standard output buffering. Since you apparently can modify the console app, try adding std::flush, something like

std::cout << "FUBAR" << std::flush;

You can also try using std::setvbuf in the console application to disable buffering all together or force line buffering, though I'd probably prefer explicit flushing if you have clear places where you have complete piece of output you can flush all at once.

This happens, because stdout is buffered differently depending wether it is running in an actual terminal/console (has a tty/pty, to be more specific) or if output is going to a pipe. With terminal, a human is probably looking at the output in real time, and line buffering is a decent compromise between realtime and throughput. With pipe, output is going to another process, which usually doesn't care when things are flushed so default is to flush only when buffers are full to minimize IO overhead, which could even be when process exits if there isn't enough output to fill the buffer.

hyde
  • 60,639
  • 21
  • 115
  • 176