gpt4 book ai didi

Python Popen输出到c程序,fget循环读取同一个stdin

转载 作者:太空宇宙 更新时间:2023-11-04 01:10:30 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com