gpt4 book ai didi

c - 将字符串写入管道

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

我正在尝试将字符串发送到 unix 中的管道。当我逐行调试过程时,调用 mkfifo() 在与源代码相同的目录中创建文件。但是,当我到达 open() 调用时,调试器不再能够继续。我不确定为什么它无法访问管道文件。

这里是有问题的代码:

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int fd;
char * myfifo = "myfifo";

/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);

/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
write(fd, "Hi", sizeof("Hi"));
close(fd);

/* remove the FIFO */
unlink(myfifo);

return 0;
}

如有任何建议,我们将不胜感激。谢谢。

最佳答案

通常情况下,FIFO 必须同时在两端打开才能继续进行。由于您没有提及任何关于阅读器的信息,最有可能的答案是您还没有阅读器,或者您还没有设置它。一旦您这样做,打开将被允许继续。

关于c - 将字符串写入管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32919476/

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