gpt4 book ai didi

c - 为什么 grep 给出 "Binary file (standard input) matches"?

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:24 25 4
gpt4 key购买 nike

#include <stdio.h>

int main()
{
FILE* cmd = popen("grep Hello", "w");
fwrite("Hello\n", 6, 6, cmd);
fwrite("Hillo\n", 6, 6, cmd);
fwrite("Hello\n", 6, 6, cmd);
pclose(cmd);
}

上面的程序输出:

Binary file (standard input) matches

为什么 grep 会给出消息,如何解决?

最佳答案

您正在尝试写入 36 个字节而不是 6 个字节,有效地访问了字符串末尾以外的字节。绝对未定义的行为。只需要第一个 '\0' 字节。

使用

fwrite("Hello\n", 1, 6, cmd);

或者更简单地说:

fputs("Hello\n", cmd);

关于c - 为什么 grep 给出 "Binary file (standard input) matches"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30721383/

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