title: Redirecting printf C++ console output to C#
body: <p>This is my method which is called when I click on a button in my C# GUI program. It launches a very simple C++ console program that does nothing but prints out a line every second in a never ending loop.</p>
  
  <pre><code>private static Process process;
  
  private void LaunchCommandLineApp()
  {
      process = new Process();
      process.StartInfo.FileName = "SimpleTest.exe";
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.RedirectStandardOutput = true;
      process.EnableRaisingEvents = true;
      process.StartInfo.CreateNoWindow = false;
      process.OutputDataReceived += process_OutputDataReceived;
  
      process.Start();
      process.BeginOutputReadLine();
  }
  </code></pre>
  
  <p>This is the method to handle any output data received:</p>
  
  <pre><code>private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
  {
      if (e.Data != null)
          Console.WriteLine(e.Data.ToString());
  }
  </code></pre>
  
  <p>I do not see any output in my C# debug output...
  But if i change the printf to std::cout, it will show the redirected message.</p>
  
  <p>I am thinking if there's any way to show those statements using printf ?</p>
  
  <p>FYI: my c++ code <strong>[EDITED working version]</strong>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
  int i = 0;
  
  for(;;)
  {
      Sleep(1000);
      i++;
      // this version of printf with fflush will work
      printf("The current value of i is %d\n", i);
      fflush(stdout);
  
      // this version of cout will also work
      //std::cout &lt;&lt; "the current value of i is " &lt;&lt; i &lt;&lt; std::endl;
  }
  
  printf("Program exit\n");
  }
  </code></pre>