title: Piping for input/output
body: <p>This question follows from my attempt to implement the instructions in:</p>
  
  <p><a href="https://stackoverflow.com/questions/1734932/linux-pipes-as-input-and-output">Linux Pipes as Input and Output</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2784500/how-to-send-a-simple-string-between-two-programs-using-pipes">How to send a simple string between two programs using pipes?</a></p>
  
  <p><a href="http://tldp.org/LDP/lpg/node11.html" rel="nofollow noreferrer">http://tldp.org/LDP/lpg/node11.html</a></p>
  
  <p>My question is along the lines of the question in: <a href="https://stackoverflow.com/questions/1734932/linux-pipes-as-input-and-output">Linux Pipes as Input and Output</a>, but more specific.</p>
  
  <p>Essentially, I am trying to replace:</p>
  
  <pre><code>/directory/program &lt; input.txt &gt; output.txt
  </code></pre>
  
  <p>using pipes in C++ in order to avoid using the hard drive. Here's my code:</p>
  
  <pre><code>//LET THE PLUMBING BEGIN 
  int fd_p2c[2], fd_pFc[2], bytes_read;
      // "p2c" = pipe_to_child, "pFc" = pipe_from_child (see above link)
  pid_t childpid;
  char readbuffer[80];
  string program_name;// &lt;---- includes program name + full path
  string gulp_command;// &lt;---- includes my line-by-line stdin for program execution
  string receive_output = "";
  
  pipe(fd_p2c);//create pipe-to-child
  pipe(fd_pFc);//create pipe-from-child
  childpid = fork();//create fork
  
  if (childpid &lt; 0)
  {
      cout &lt;&lt; "Fork failed" &lt;&lt; endl;
      exit(-1);
  }
  else if (childpid == 0)
  {
      dup2(0,fd_p2c[0]);//close stdout &amp; make read end of p2c into stdout
      close(fd_p2c[0]);//close read end of p2c
      close(fd_p2c[1]);//close write end of p2c
      dup2(1,fd_pFc[1]);//close stdin &amp; make read end of pFc into stdin
      close(fd_pFc[1]);//close write end of pFc
      close(fd_pFc[0]);//close read end of pFc
  
      //Execute the required program
      execl(program_name.c_str(),program_name.c_str(),(char *) 0);
      exit(0);
  }
  else
  {
      close(fd_p2c[0]);//close read end of p2c
      close(fd_pFc[1]);//close write end of pFc
  
      //"Loop" - send all data to child on write end of p2c
      write(fd_p2c[1], gulp_command.c_str(), (strlen(gulp_command.c_str())));
      close(fd_p2c[1]);//close write end of p2c
  
      //Loop - receive all data to child on read end of pFc
      while (1)
      {        
          bytes_read = read(fd_pFc[0], readbuffer, sizeof(readbuffer));
  
          if (bytes_read &lt;= 0)//if nothing read from buffer...
              break;//...break loop
  
          receive_output += readbuffer;//append data to string
      }
      close(fd_pFc[0]);//close read end of pFc
  }
  </code></pre>
  
  <p>I am absolutely sure that the above strings are initialized properly. However, two things happen that don't make sense to me:</p>
  
  <p>(1) The program I am executing reports that the "input file is empty." Since I am not calling the program with "&lt;" it should not be expecting an input file. Instead, it should be expecting keyboard input. Furthermore, it should be reading the text contained in "gulp_command."</p>
  
  <p>(2) The program's report (provided via standard output) appears in the terminal. This is odd because the purpose of this piping is to transfer stdout to my string "receive_output." But since it is appearing on screen, that indicates to me that the information is not being passed correctly through the pipe to the variable. If I implement the following at the end of the if statement,</p>
  
  <pre><code>cout &lt;&lt; receive_output &lt;&lt; endl;
  </code></pre>
  
  <p>I get nothing, as though the string is empty. I appreciate any help you can give me!</p>
  
  <p>EDIT: Clarification</p>
  
  <p>My program currently communicates with another program using text files. My program writes a text file (e.g. input.txt), which is read by the external program. That program then produces output.txt, which is read by my program. So it's something like this:</p>
  
  <pre><code>my code -&gt; input.txt -&gt; program -&gt; output.txt -&gt; my code
  </code></pre>
  
  <p>Therefore, my code currently uses,</p>
  
  <pre><code>system("program &lt; input.txt &gt; output.txt");
  </code></pre>
  
  <p>I want to replace this process using pipes. I want to pass my input as standard input to the program, and have my code read the standard output from that program into a string.</p>