Python Popen 输出到 C 程序,fget 在循环中读取相同的 stdin

标签 python c subprocess popen fgets

我希望 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/

相关文章:

c++ - 声明为 volatile 的 InterlockedIncrement 参数有什么影响

c - C atoi 函数中的潜在错误

python - 打开特定目录时,通过 python 打开 gnome 终端似乎不起作用

python - 如何使用 Python 运行子进程,等待它退出并以字符串形式获取完整的标准输出?

python - 在 Windows 10 上运行 python 脚本

python - 如何识别一个字节字符串是否是一个 pickled 对象?

Python:将 Shapely 对象导出到 DXF 文件

Python在html元素末尾查找文本

python - 如何将 python 连接到 Oracle Application Express

c - 为什么局部变量不匹配全零条件?