'Double pipe to save output to file in C

I have created something like "linux pipe" to redirect output from one program to another and it's working like a charm. But now I want to save final output to a file. On the internet I saw that I need to create double pipe, but everything I create is not working.

Here is my actual code of "pipe":

 int pipes[2];
 int pid;
 pipe(pipes);
 if(pid=fork()){
         int fd = open("t.txt", O_CREAT | O_TRUNC | O_RDWR, 0644);
    if (fd < 0) {
        perror("open()");
        exit(EXIT_FAILURE);
    }
   close(pipes[1]);
   dup2(pipes[0],STDIN_FILENO);
    execvp(sec_command[0],sec_command);

 }
     else{

   close(pipes[0]);
   dup2(pipes[1],STDOUT_FILENO);
   execvp(first_command[0],first_command);
 }

Thanks

c


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source