Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用子进程写入标准数据_Python_Linux - Fatal编程技术网

Python 使用子进程写入标准数据

Python 使用子进程写入标准数据,python,linux,Python,Linux,我有下面的C程序 #include <stdio.h> int main() { char a[200]; a[199] = 0; printf("Enter some input ->\n"); scanf("%s" ,a); printf ("\nInput was %s\n", a); return 0; } 但这似乎不起作用。。。。有没有办法解决这个问题 **编辑** 有人知道为什么a.stdin.

我有下面的C程序

#include <stdio.h>


int main()
{

    char a[200];
    a[199] = 0;


    printf("Enter some input ->\n");
    scanf("%s"  ,a);

    printf ("\nInput was %s\n", a);

    return 0;
}   
但这似乎不起作用。。。。有没有办法解决这个问题

**编辑**


有人知道为什么
a.stdin.flush()
不起作用吗?

scanf
读取一整行,直到它读取新行字符
\n
,因此您也必须发送此信息:

from subprocess import Popen, PIPE
a = Popen(["my_prog.elf"], stdin=PIPE) 
a.stdin.write("MyInput\n")

仅当输出流(
stdin
)被缓冲时(默认情况下,这些管道不被缓冲),才需要使用
flush
。即使如此,您仍需要
\n
来终止该行。

**编辑**哇。。。。这显然是出于某种原因。。。。。。。。。。。。。知道为什么同花顺不管用吗?这是管用的。你到底做了什么?你知道谁,它不起作用了?基本上我在写stdin,并且有我用stdin写的东西的打印,所以当尝试使用flush时,不幸失败了@DanielWell,实际上,
scanf(“%s”…)
读取一整行,因为这就是“字符串”如何定义为
scanf
。从技术上讲,更正确的说法是,
scanf
将一直读取到与当前处理的格式标志指定的数据类型不兼容的第一个字符,例如,
%d
将在第一个非十进制数字处停止。。。
from subprocess import Popen, PIPE
a = Popen(["my_prog.elf"], stdin=PIPE) 
a.stdin.write("MyInput\n")