0

I am struggling with a problem. I have Python program which send array to C++ exe . However I can't receive array from C++. My python code:

import struct
import subprocess
from cStringIO import StringIO

stdin_buf = StringIO()
array = [1.0 for _ in range(10)]
for item in array:
stdin_buf.write(struct.pack('<f', item))

proc = subprocess.Popen(['Comsol1.exe'], stdin=subprocess.PIPE, stdout = subprocess.PIPE)
out, err = proc.communicate(stdin_buf.getvalue())

# assuming the result comes back the same way it went in...
item_len = struct.calcsize('<f')
stdout_buf = StringIO(out)
stdout_buf.seek(0)
for i in range(len(out)/item_len):
   val = struct.unpack('<f', stdout_buf.read(4))
   print (val)

C++ code:

// Comsol1.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include <streambuf>
#include "stdafx.h"
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>

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<char*>(&value), sizeof(value));
    if (std::cin.gcount() > 0)
    {
        std::cerr << "Car " << i << ": " << value << std::endl;
        i++;
    }
}
      }

Thank you.

6
  • I'm not sure what exactly is the problem. I ran this code on Ubuntu (of course after removing the _setmode calls which are not available and obsolete on linux). It seems to work. I received 10 Car i: 1 lines back. Do you have any errors? Commented Jun 11, 2014 at 16:04
  • Yes it works in one way from Python to C++ ( so we print it cout in C++ code). But I want to print the result of test.exe in Python code like this print(val). val is empty here. it looks like I didn't receive my array back . Commented Jun 11, 2014 at 16:50
  • @Jack - your c++ program isn't writing to cout. Commented Jun 11, 2014 at 16:58
  • I think have to redirect my array in stdout C++ . I tried to do this but it doesn't work . That is the real question Commented Jun 11, 2014 at 17:06
  • the second part of Python code doesn't work . I don't receive my array from exe file. Commented Jun 11, 2014 at 17:11

1 Answer 1

1

So you have two problems here:

  1. You are printing to stderr instead of stdout, and because stderr is not piped you'll actually get the messages printed to the console when you run your python script.

  2. You are printing more than just floats to stdout and not in raw binary mode. If you expect to read a list of floats from out (in python) you have to print only the floats (in c++) and in binary mode:

    std::cout.write(reinterpret_cast<const char*>(&value), sizeof(value));

I tried the above approach on Ubuntu and it's working properly. You can find my source code here. I had to tweak the code to work on unix, but you can get the idea.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.