python子进程与c++;程序 是否可以使用Python子进程与使用UNITSTD、H/CODE的读(C++)程序通信?当C++程序运行时,输入的数据会被存储在文件中。当我从python调用它并尝试编写数据时,它似乎并没有捕获它 pyth
python子进程与c++;程序python子进程与c++;程序 是否可以使用Python子进程与使用UNITSTD、H/CODE的读(C++)程序通信?当C++程序运行时,输入的数据会被存储在文件中。当我从python调用它并尝试编写数据时,它似乎并没有捕获它 pyth,python,c++,subprocess,ipc,Python,C++,Subprocess,Ipc,python子进程与c++;程序 是否可以使用Python子进程与使用UNITSTD、H/CODE的读(C++)程序通信?当C++程序运行时,输入的数据会被存储在文件中。当我从python调用它并尝试编写数据时,它似乎并没有捕获它 python代码 C++程序 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 std::流文件的输出; volatile bool ctrlc_pressed=假; 静态标准::字符串读取行(int-fd) { 结构termios ti
是否可以使用Python子进程与使用UNITSTD、H/CODE的<代码>读(C++)程序通信?当C++程序运行时,输入的数据会被存储在文件中。当我从python调用它并尝试编写数据时,它似乎并没有捕获它
python代码 C++程序#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
std::流文件的输出;
volatile bool ctrlc_pressed=假;
静态标准::字符串读取行(int-fd)
{
结构termios tios;
bool noncanonical=tcgetattr(fd和tios)==0&(tios.c_lflag和ICANON)==0;
std::字符串s;
用于(字符读取(fd,&ch,1)==1;)
{
如果(ch='\x7f')
{
如果(s.empty())
继续;
s、 擦除(s.end()-1);
if(非规范和写入(fd,“\b\b”,3)!=3)
闭嘴
}
else if(非规范和写入(fd和ch,1)!=1)
闭嘴
如果(ch='\n')
打破
如果(ch!='\x7f')
s+=ch;
}
返回s;
}
int main()
{
文件_out.open(“out.txt”);
文件输出如果您试图从控制台输出读取,请尝试使用check\u output
而不是Popen
。Doc:std::string s=readline(2)也许你是对的,但是我不是C++程序的作者。这个C++程序以前是否成功地使用过?如果不是,我建议如果可能的话,联系作者,或者自己做修改,看看它是否有任何改变。区别。除非我真的有一个休息的时刻,否则这句话永远不会读到任何东西。
#!/usr/bin/env python3
import sys
import pdb
import subprocess
import time
proc = subprocess.Popen(['./std.bin'],stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,shell=True)
time.sleep(1)
for cnt in range(100):
proc.stdin.write(b'test1\ntest2\n')
proc.stdin.flush()
proc.stdin.write(b"qq")
#include <termios.h>
#include <map>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <sstream>
#include <string>
#include <fstream>
std::ofstream file_out;
volatile bool ctrlc_pressed = false;
static std::string readline(int fd)
{
struct termios tios;
bool noncanonical = tcgetattr(fd, &tios) == 0 && (tios.c_lflag & ICANON) == 0;
std::string s;
for (char ch; read(fd, &ch, 1) == 1; )
{
if (ch == '\x7f')
{
if (s.empty())
continue;
s.erase(s.end()-1);
if (noncanonical && write(fd, "\b \b", 3) != 3)
; // shut up gcc
}
else if (noncanonical && write(fd, &ch, 1) != 1)
; // shut up gcc
if (ch == '\n')
break;
if (ch != '\x7f')
s += ch;
}
return s;
}
int main()
{
file_out.open("out.txt");
file_out<<"Started\n"<<std::flush;
while (1)
{
std::cerr << ": " << std::flush;
std::string s = readline(2);
std::stringstream ss(s);
std::string cmd, tmp;
if (!(ss >> cmd))
{
continue;
}
if(cmd.find('q')!=std::string::npos) return 0;
std::cout<<cmd;
file_out<<cmd<<std::flush;
while (ss >> tmp)
{
std::cout<<' '<<tmp;
file_out<<' '<<tmp<<std::flush;
}
file_out<<'\n'<<std::flush;
std::cout<<'\n'<<std::flush;
}
return 0;
}