title: How does popen() work and how to implement it into c++ code on linux?
body: <p>I am having trouble with finding out how to use <strong>popen()</strong> to grab stdout from child programs in Linux to a main C++ program. I was looking around and found this snippet of code that does what I want it to do. But I can't understand how this stuff works. I know the basics of c++ programming (I've been doing it for several months now) but i'm stumped so can someone help me with an explanation of this?</p>
  
  <p>Thank you in advance.</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  void my_popen( const string&amp; cmd ,vector&lt;string&gt;&amp; out ){
      FILE*  fp;
      const int sizebuf=1234;
      char buff[sizebuf];
      out = vector&lt;string&gt; ();
      if ((fp = popen (cmd.c_str (), "r"))== NULL){
  
  }
      string cur_string = "";
      while (fgets(buff, sizeof (buff), fp)) {
          cur_string += buff;
  }
      out.push_back (cur_string.substr (0, cur_string.size() -1));
      pclose(fp);
  }
  
  
  
  
  int main () {
  
      vector&lt;string&gt; output;
      my_popen("which driftnet", output);
      for (vector&lt;string&gt;::iterator itr = output.begin();
          itr != output.end();
          ++itr) {
              cout &lt;&lt; *itr &lt;&lt; endl;
          }
      return 0;
  }
  </code></pre>