title: Run process with string input and output
body: <p>There are plenty of questions on here related to fork() and exec().  I have not found one that really makes the process of using them simple though, and making programmer's lives simple is the goal.  </p>
  
  <p>I need a C++, linux-friendly function that does the following:</p>
  
  <pre><code>string RunCommand(string command, string input){}
  </code></pre>
  
  <p>This function should be able to run a shell command, like grep, and "pipe" the content of input into it and read the ouptut and return it.  So if I would do the following at the command line:</p>
  
  <pre><code>ps -elf | grep somequerytext
  </code></pre>
  
  <p>I would in code do:</p>
  
  <pre><code>string psOutput = RunCommand("ps -elf","");
  string grepOutput = RunCommand("grep somequerytext", psOutput);
  </code></pre>
  
  <p>*edit:  The question is what is the best implementation of the RunCommand function.</p>
  
  <p>*edit:  popen was considered as a solution for simplicity, but popen restricts you to piping data in or piping data out, but not both.</p>