title: unsuccessful use of popen() in C?
body: <p>I can run the following command</p>
  
  <p><code>xwd -root | xwdtopnm | pnmtojpeg &gt; screen.jpg</code></p>
  
  <p>in a terminal under linux and it will produce a screenshot of my current screen.</p>
  
  <p>I try to do the following with the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int main()
  {
     FILE *fpipe;
     char *command="xwd -root | xwdtopnm | pnmtojpeg";
     char line[256];
  
     if ( !(fpipe = (FILE*)popen(command,"r")) )
     {  // If fpipe is NULL
        perror("Problems with pipe");
        exit(1);
     }
  
     while ( fgets( line, sizeof line, fpipe))
     {
        //printf("%s", line);
        puts(line);
     }
     pclose(fpipe);
  }
  </code></pre>
  
  <p>then I compile and run the program <code>./popen &gt; screen.jpg</code> but the resulting file screen.jpg is unrecongizable. How can I do this so that I can pipe through my program correctly?</p>