title: How can I read process output that has not been flushed?
body: <p>Consider this little programm be compiled as <code>application.exe</code></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main()
  {
      char str[100];
      printf ("Hello, please type something\n");
      scanf("%[^\n]s", &amp;str);
      printf("you typed: %s\n", str);
      return 0;
  }
  </code></pre>
  
  <p>Now I use this code to start <code>application.exe</code> and fetch its output.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdexcept&gt;
  
  int main()
  {
      char buffer[128];
      FILE* pipe = popen("application.exe", "r");
      while (!feof(pipe)) {
          if (fgets(buffer, 128, pipe) != NULL)
              printf(buffer);
      }
      pclose(pipe);
      return 0;
  }
  </code></pre>
  
  <p>My problem is that there is no output until I did my input. Then both output lines get fetched. 
  I can workarround this problem by adding this line after the first <code>printf</code> statement.</p>
  
  <pre><code>fflush(stdout);
  </code></pre>
  
  <p>Then the first line is fetched before I do my input as expected.</p>
  
  <p><strong>But</strong> how can I fetch output of applications that I cannot modify and that do not use <code>fflush()</code> in "realtime" (means before they exit)? . 
  And how does the windows cmd do it?</p>