title: Pipe output of a c++ program to another program
body: <p><strong>main.cpp</strong>
  Takes 2 command line arguments and opens file named "exampleTest.txt" and writes to that file. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  int main(int argc, char const *argv[])
  {
      ofstream op;
      op.open("exampleTest.txt",ios::out);
      op&lt;&lt;argv[1]&lt;&lt;"\n"&lt;&lt;argv[2]&lt;&lt;endl;
      op.close();
      return 0;
  }
  </code></pre>
  
  <p><strong>pgm1.cpp</strong> OPens the file named "exampleTest.txt" and displays the contents of that file</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  using namespace std;
  int main(int argc, char const *argv[])
  {
      string line;
      ifstream fs;
      fs.open("exampleTest.txt",ios::in);
  
      if (fs.is_open())
      {
          while(getline(fs,line))
          {
              cout&lt;&lt;line&lt;&lt;endl;
  
          }
      }
      fs.close();
  
      return 0;
  }
  </code></pre>
  
  <p>By compiling the 2 programs and running them individually works just fine.
  However, i want to pipe the output the main.cpp output to the program pgm1.cpp</p>
  
  <p>What i tried:</p>
  
  <pre><code>g++ main.cpp -o main
  g++ pgm1.cpp -o pgm
  ./pgm |./main abc def
  </code></pre>
  
  <p>What i expect the output to be:</p>
  
  <pre><code>abc 
  def
  </code></pre>
  
  <p>What i am getting:</p>
  
  <pre><code>&lt;no output&gt;
  </code></pre>
  
  <p>My reasoning:
  the 'main' creates the file 'exampleTest.txt' with abc def as the input to the file.
  While this file is given to the 'pgm' (piped to...), it should be able to read the file contents and output </p>
  
  <pre><code>abc
  def
  </code></pre>
  
  <p>in the terminal, instead i am not getting any output. (Works if i run each of the file separately )<br>
  Could you please let me know what am i missing?</p>