Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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 stdout.flush()不刷新最后一个白色字符,如'\n';_Python_Stdout_Stdin_Flush - Fatal编程技术网

python stdout.flush()不刷新最后一个白色字符,如'\n';

python stdout.flush()不刷新最后一个白色字符,如'\n';,python,stdout,stdin,flush,Python,Stdout,Stdin,Flush,我有两个节目。用C++编写的第一个(subprocess.cpp): #include <stdio.h> int main() { char * line = new char[1000]; // first scan scanf("%s\n", line); printf("%s\n", line); fflush(stdout); // second scan scanf("%s\n", line); pr

我有两个节目。用C++编写的第一个(
subprocess.cpp
):

#include <stdio.h>

int main() {

    char * line = new char[1000];

    // first scan
    scanf("%s\n", line);
    printf("%s\n", line);
    fflush(stdout);

    // second scan
    scanf("%s\n", line);
    printf("%s\n", line);
    fflush(stdout);

    return 0;
}
此脚本应启动子访问,发送和检索两个字符串。看看发生了什么事:

marcin@marcin-Aspire-7540 ~/Desktop/inzynierka $ g++ subprocess.cpp -o subprocess
marcin@marcin-Aspire-7540 ~/Desktop/inzynierka $ python test.py 
Subprocess line 1 :: abc
test.py
正在等待程序
子进程的第二行。程序
子进程
无法发送第二个字符串,因为它正在等待'\n'字符

pid 6611: test.py
pid 6613: subprocess

> strace -f python test.py
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6613] read(0, "abc\nxyz\n", 4096) = 8
...
[pid  6613] write(1, "abc\n", 4)        = 4
[pid  6611] <... read resumed> "a", 1)  = 1
[pid  6613] read(0,  <unfinished ...>
[pid  6611] read(5, "b", 1)             = 1
[pid  6611] read(5, "c", 1)             = 1
[pid  6611] read(5, "\n", 1)            = 1
...
[pid  6611] write(1, "Subprocess line 1 :: abc\n", 25Subprocess line 1 :: abc) = 25
[pid  6611] write(1, "\n", 1)           = 1
[pid  6611] read(5, 
如果我将
subprocess.cpp
中的第二个
scanf
更改为
scanf(“%s\n”,行)(不
\n
)一切正常。当我在
test.py
中再发送一行时也会发生同样的情况:

# send initial data
my_process.stdin.write('abc\n')
my_process.stdin.flush()
my_process.stdin.write('xyz\n')
my_process.stdin.flush()
my_process.stdin.write('ADDITIONAL\n')
my_process.stdin.flush()
Python似乎没有在刷新后发送最后\n个字符(请参见更改了scanf的示例)。在
test.py
中添加一个额外的写入和刷新可以证明,在刷新之后,缺少的\n字符仍然在缓冲区中


那怎么办?如何使Python的flush刷新所有字符?

错误在于使用i格式字符串的scanf行为。在使用管道时,scanf(“%s\n”)将在\n之后的第一个非空白字符之后返回,或者在使用终端时在第一个非空行之后返回(因为第一个非空白字符将在下一个\n之后刷新)

关键是您可能不是有意使用scanf(“%s\n”)。我猜你真正想要的是:

scanf("%s%*[^\n]", line);

它将读取word,然后丢弃同一行中的所有字符。

错误在于\n i格式字符串的扫描行为。在使用管道时,scanf(“%s\n”)将在\n之后的第一个非空白字符之后返回,或者在使用终端时在第一个非空行之后返回(因为第一个非空白字符将在下一个\n之后刷新)

关键是您可能不是有意使用scanf(“%s\n”)。我猜你真正想要的是:

scanf("%s%*[^\n]", line);

它将读取单词,然后丢弃同一行中的所有字符。

问题在于
scanf(“%s\n”,line)
,第二个
scanf()
需要读取另一行才能找到非空白字符

pid 6611: test.py
pid 6613: subprocess

> strace -f python test.py
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6613] read(0, "abc\nxyz\n", 4096) = 8
...
[pid  6613] write(1, "abc\n", 4)        = 4
[pid  6611] <... read resumed> "a", 1)  = 1
[pid  6613] read(0,  <unfinished ...>
[pid  6611] read(5, "b", 1)             = 1
[pid  6611] read(5, "c", 1)             = 1
[pid  6611] read(5, "\n", 1)            = 1
...
[pid  6611] write(1, "Subprocess line 1 :: abc\n", 25Subprocess line 1 :: abc) = 25
[pid  6611] write(1, "\n", 1)           = 1
[pid  6611] read(5, 
类似的情况有很好的解释

我还用
strace
检查了它,Python发送最后一个
'\n'
字符

pid 6611: test.py
pid 6613: subprocess

> strace -f python test.py
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6613] read(0, "abc\nxyz\n", 4096) = 8
...
[pid  6613] write(1, "abc\n", 4)        = 4
[pid  6611] <... read resumed> "a", 1)  = 1
[pid  6613] read(0,  <unfinished ...>
[pid  6611] read(5, "b", 1)             = 1
[pid  6611] read(5, "c", 1)             = 1
[pid  6611] read(5, "\n", 1)            = 1
...
[pid  6611] write(1, "Subprocess line 1 :: abc\n", 25Subprocess line 1 :: abc) = 25
[pid  6611] write(1, "\n", 1)           = 1
[pid  6611] read(5, 
pid 6611:test.py pid 6613:子流程 >strace-f python test.py [pid 6611]写入(4,“abc\n”,4 ... [pid 6611]写入(4,“abc\n”,4 ... [pid 6613]读取(0,“abc\nxyz\n”,4096)=8 ... [pid 6613]写入(1,“abc\n”,4)=4 [pid 6611]“a”,1)=1 [pid 6613]读取(0, [pid 6611]读取(5,“b”,1)=1 [pid 6611]读取(5,“c”,1)=1 [pid 6611]读取(5,“\n”,1)=1 ... [pid 6611]写入(1,“子流程行1::abc\n”,25子流程行1::abc)=25 [pid 6611]写入(1,“\n”,1)=1 [pid 6611]读取(5,
问题在于
scanf(“%s\n”,line)
,第二个
scanf()
需要读取另一行才能找到非空白字符

pid 6611: test.py
pid 6613: subprocess

> strace -f python test.py
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6613] read(0, "abc\nxyz\n", 4096) = 8
...
[pid  6613] write(1, "abc\n", 4)        = 4
[pid  6611] <... read resumed> "a", 1)  = 1
[pid  6613] read(0,  <unfinished ...>
[pid  6611] read(5, "b", 1)             = 1
[pid  6611] read(5, "c", 1)             = 1
[pid  6611] read(5, "\n", 1)            = 1
...
[pid  6611] write(1, "Subprocess line 1 :: abc\n", 25Subprocess line 1 :: abc) = 25
[pid  6611] write(1, "\n", 1)           = 1
[pid  6611] read(5, 
类似的情况有很好的解释

我还用
strace
检查了它,Python发送最后一个
'\n'
字符

pid 6611: test.py
pid 6613: subprocess

> strace -f python test.py
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6611] write(4, "abc\n", 4 <unfinished ...>
...
[pid  6613] read(0, "abc\nxyz\n", 4096) = 8
...
[pid  6613] write(1, "abc\n", 4)        = 4
[pid  6611] <... read resumed> "a", 1)  = 1
[pid  6613] read(0,  <unfinished ...>
[pid  6611] read(5, "b", 1)             = 1
[pid  6611] read(5, "c", 1)             = 1
[pid  6611] read(5, "\n", 1)            = 1
...
[pid  6611] write(1, "Subprocess line 1 :: abc\n", 25Subprocess line 1 :: abc) = 25
[pid  6611] write(1, "\n", 1)           = 1
[pid  6611] read(5, 
pid 6611:test.py pid 6613:子流程 >strace-f python test.py [pid 6611]写入(4,“abc\n”,4 ... [pid 6611]写入(4,“abc\n”,4 ... [pid 6613]读取(0,“abc\nxyz\n”,4096)=8 ... [pid 6613]写入(1,“abc\n”,4)=4 [pid 6611]“a”,1)=1 [pid 6613]读取(0, [pid 6611]读取(5,“b”,1)=1 [pid 6611]读取(5,“c”,1)=1 [pid 6611]读取(5,“\n”,1)=1 ... [pid 6611]写入(1,“子流程行1::abc\n”,25子流程行1::abc)=25 [pid 6611]写入(1,“\n”,1)=1 [pid 6611]读取(5,
同意,问题是在scanf格式字符串中使用“\n”,而不是在Python中。实际上,只要
scanf(“%s”,line)
就可以了,甚至
get(line)
(取决于意图)。谢谢,我不知道问题不在Python本身。同意,问题是在scanf格式字符串中使用“\n”,而不是在Python中。实际上,只要
scanf(“%s”,line)
就可以了,甚至
获取(line)
(取决于意图)。谢谢,我不知道问题不在Python本身。