파이썬에서 C++로 배열을 전달하고 stdout 및 stdin을 사용하여 다시 전달해야합니다. C++에 전달할 수 있습니다. 그러나 나는 파이썬으로 다시 보낼 수 없다. stdout 모드를 설정하는 방법을 이해할 수 없다고 생각합니다. 제게 조언 해주세요. 고맙습니다. 내 파이썬 코드 :파이썬 및 C++ 통신 stdout 및 stdin
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('<f', item))
data = bytes()
while len(data) < 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()
MY C++ 코드 :
#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++;
}
if (std::cin.gcount() > 0)
{
std::cerr << "Car " << i << ": " << value << std::endl;
std::cout.write(reinterpret_cast<char*>(&value), sizeof(value));
i++;
}
}
}
제대로 작동하지 않습니까? 무엇을 기대합니까? 대신 무엇을합니까? – univerio
파이썬 코드에서 stdout을 읽지 않으려 고하는 것 같습니다. 뭔가 다른 걸 기대 했니? – univerio
죄송합니다. 제 코드를 수정했습니다. 그것은 불행하게도 작동하지 않습니다. – 23454321