gpt4 book ai didi

tee 可以在 c 编程中将 fifo 重定向到 stdout 吗?

转载 作者:IT王子 更新时间:2023-10-29 01:25:40 26 4
gpt4 key购买 nike

我想将一个 fifo 重定向到 stdout 并且我阅读了文档 http://man7.org/linux/man-pages/man2/tee.2.html

上面写着tee(int fd_in, int fd_out,...)

但是当我将 fifo fd 抛给第一个参数时,它说无效错误。

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
int main() {
int num = 0, fd;
char fifo[] = "/tmp/tmpfifo";

fd = open(fifo, O_RDONLY, 0644);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
num = tee(fd, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK);
if (num < 0) {
perror("tee");
exit(EXIT_FAILURE);
}
fprintf(stderr,"%d\n", num);
return 0;
}

控制台显示:tee:invalid arguments。第一个参数应该是标准输入?

最佳答案

确保您的 stdout 是管道。

rm -f /tmp/tmpfifo
mkfifo /tmp/tmpfifo
echo hello world > /tmp/tmpfifo &
./a.out | cat #ensure that the program's stdout is a pipe

(其中 a.out 是您的程序)适合我。

关于tee 可以在 c 编程中将 fifo 重定向到 stdout 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45752606/

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