Can redirect fifo in stdout to c-programming?

I want to redirect fifo to stdout and I read the document http://man7.org/linux/man-pages/man2/tee.2.html

It says tee(int fd_in, int fd_out,...)

but when I throw ffo fd into the 1st argument, it says an invalid error.

#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;
}

The console shows: tee: invalid arguments. The first argument should be stdin?

+4
source share
2 answers

From the tee()man pages:

tee () duplicates up to len bytes of data from the pipe referenced by the file descriptor fd_in to the channel referenced by the file descriptor fd_out.

So, both file descriptors should refer to pipes.

tee():

tee(fd, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK);

fd - fifo, , , , STDOUT_FILENO .

STDIN_FILENO STDOUT_FILENO .


, STDOUT_FILENO , :

yourProgram | cat
+1

, stdout - .

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

( a.out - ) .

+2

Source: https://habr.com/ru/post/1683943/


All Articles