title: Python and C++ communication stdout and stdin
body: <p>I have to pass array from  Python to  C++  and back using  stdout and stdin.  I  can pass it to C++ . However I cant send it back to python.  I think I don't understand how to set stdout mode. Please give me advice. Thank you.
  My  Python code:</p>
  
  <pre><code>import struct
  import subprocess
  #import msvcrt
  #import os
  import random
  
  array = [1.0 for _ in range(10)]
  proc = subprocess.Popen(['test.exe'], stdin=subprocess.PIPE, stdout = subprocess.PIPE)
  for item in array:
  proc.communicate(struct.pack('&lt;f', item))
  
  data = bytes()
  while len(data) &lt; len(array)*4:
  data = data + proc.communicate()[0]
  
  print (len(data))
  
  #print('Python:')
  #msvcrt.setmode (proc.stdout.fileno(), os.O_BINARY)
  proc.stdin.close()
  proc.stdout.close()
  
  proc.wait()
  </code></pre>
  
  <p>MY C++ code:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;io.h&gt;
  #include &lt;iostream&gt;
  
  int main(void)
  {
  int result;
  
  // Set "stdin" to have binary mode:
  result = _setmode(_fileno(stdin), _O_BINARY);
  if (result == -1)
  perror("Cannot set mode");
  else
  fprintf(stderr, "'stdin' successfully changed to binary mode\n");
  
  // Set "stdout" to have binary mode:
  result = _setmode(_fileno(stdout), _O_BINARY);
  if (result == -1)
  perror("Cannot set mode");
  else
  
  fprintf(stderr, "'stdout' successfully changed to binary mode\n");
  
  int i = 0;
  while (!std::cin.eof())
  {
  float value;
  std::cin.read(reinterpret_cast&lt;char*&gt;(&amp;value), sizeof(value));
  if (std::cin.gcount() &gt; 0)
  {
  std::cerr &lt;&lt; "Car " &lt;&lt; i &lt;&lt; ": " &lt;&lt; value &lt;&lt; std::endl;
  i++;
  }
  if (std::cin.gcount() &gt; 0)
  {
  std::cerr &lt;&lt; "Car " &lt;&lt; i &lt;&lt; ": " &lt;&lt; value &lt;&lt; std::endl;
  std::cout.write(reinterpret_cast&lt;char*&gt;(&amp;value), sizeof(value));
  i++;
  }
  }
  }
  </code></pre>