title: popen vs system function in C
body: <p>Is The benefit of using popen is only to read the ouput produced by a command or there are some more benefits or advantages of popen over system.</p>
  
  <p>Consider two programs below:</p>
  
  <p>Program 1:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main()
  {
      FILE *more,*who;
  
      if (!(more = popen("more", "w")))
      {
              printf("Command `more` not found!");
              return -1;
      }
      if (!(who = popen("who", "r")))
      {
              printf("Command `who` not found!");
              return -1;
      }
  
      while (!feof(who))
      {
              char buffer[100];
              if (fgets(buffer, 100, who) != NULL)
              {
                      fputs(buffer, more);
              }
      }
  
      fclose(more);
      fclose(who);
    return 0;
   }
  </code></pre>
  
  <p>Program 2:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  int main()
  {
      system("who|more");
      return 0;
  }
  </code></pre>
  
  <p>Why should i use <strong>Program1</strong> if i can do the same thing in one line as done in <strong>Program2</strong>.</p>