c - Writing to file descriptor -


in following snippet redirecting output of ls command input of wc -l works .now want redirect output of ls command file named "beejoutput.txt" using following code not working. need help.

#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) {   int pfds[2];   pipe(pfds);   if (!fork())   {     dup2(pfds[1],1);     close(pfds[0]);      execlp("ls", "ls",null);   }   else   {     file *outputo=fopen ("beejoutput.txt", "w"); //opening file writing      dup2(pfds[0],0);     dup2(fileno(outputo),pfds[0]);      close(pfds[1]);      execlp("wc", "wc","-l", null);   }    return 0; } 

the dup function duplicates file descriptor, is, both old , new file descriptors refer same open file afterwards. different having single file descriptor refer 2 different files @ same time.

if want send same data 2 different destinations, need spawn both commands in separate processes, , copying yourself, or spawn copy of "tee" command -- either way, end 3 processes.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -