title: C++ How to read in a text file and reverse the lines so it reads from bottom up
body: <p>Please help I'm new to C++</p>
  
  <p>Using stacks Implementation</p>
  
  <p>I should read a sample text file containing:</p>
  
  <pre><code>text file
  begining to end
  return text
  </code></pre>
  
  <p>And return a text file reading:</p>
  
  <pre><code>return text
  begining to end
  test file
  </code></pre>
  
  <p>This is the sample code to test the implementation:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include "stack1.h"
  using namespace std
  
  void reverse (ifstream &amp; infile, ofstream &amp; outfile);  //function prototype
  
  int main () {
  
  ifstream infile;  // declaring a file for input
  ofstream outfile; // declaring a file for output
  infile.open ("c:\\myInputFile.txt");  
  
   if (!infile)  { cout &lt;&lt; "File could not be opened.  Program terminated." &lt;&lt; endl;
             return 1;    // terminates program
           }
   outfile.open ("c:\\myOutfile.txt");
   reverse (infile, outfile);
   infile.close();
  outfile.close();
   return 0;
   }
  
  void reverse (ifstream &amp; infile, ofstream &amp; outfile) {
   string s;
   stack&lt;string&gt; mystack;
  
   getline(infile, s); // try to read a line of text from the file
   while (!infile) { // while not end of file
    mystack.push(s);
    getline(infile, s); // try to read another line
    }
   while (! mystack.isEmpty() ) {
    s = mystack.pop();
    outfile &lt;&lt; s &lt;&lt; endl; // write s to the outfile
    }
   }
  </code></pre>