我希望 C 程序打印收到的 3 行。但结果是c程序不停地打印from c program:33333333
。我不知道为什么fgets()
执行后没有消耗stdin
。
# pin.py
from subprocess import Popen, PIPE
p = Popen("/home/jchn/pstdin",stdin=PIPE,stdout=None)
p.stdin.write("11111111")
p.stdin.write("22222222")
p.stdin.write("33333333")
pstdin.c的内容
# pstdin.c
#include <stdio.h>
int main(){
char a[10];
FILE* fd = fopen("output","w");
while (1){
fgets(a,10,stdin);
printf("--from c program--:%s",a);
}
}
最佳答案
while(1)
是一个无限循环,没有停止条件
while(fgets(a,10,stdin) != NULL)
{
printf("--from c program--:%s",a);
}
关于Python Popen 输出到 C 程序,fget 在循环中读取相同的 stdin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049521/