title: Input redirection using dup2()
body: <p>So I am trying to implement the following command line statement in c++ by using dup2() and execvp(): <code>wc &lt; inputFile.txt</code> then return to my command line. So basically I am forking a process and executing that command in the child process.
  However my code the following error: <code>wc: stdin: read: Bad file descriptor</code></p>
  
  <p>Here is my code:</p>
  
  <pre><code>int file_desc = open(fileName.c_str(), O_WRONLY | O_APPEND); 
  int stdin = dup(0);
  dup2(file_desc,0); 
  execvp (args2[0], args2); // now execute
  dup2(stdin, 0);
  </code></pre>
  
  <p>So my thought process was that I needed to redirect the standard in (aka index 0 of the file descriptor table) to the file descriptor of the file since at index is always stdin and that's where input is read from. Then after I execute, I replace it back with the original standard in. So I am confused about what I am doing wrong.</p>