title: Writing to file descriptor
body: <p>In the following snippet i am redirecting the output of the <code>ls</code> command to input of <code>wc -l</code> which works perfectly .Now i also want to redirect the output of <code>ls</code> command to a file named "beejoutput.txt" using the following code but its not working. Need help.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  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 for writing
  
      dup2(pfds[0],0);
      dup2(fileno(outputO),pfds[0]); 
      close(pfds[1]); 
      execlp("wc", "wc","-l", NULL);
    }
  
    return 0;
  }
  </code></pre>