title: C++ Reading file backwards from the end of the file
body: <p>I am trying to write a program with a menu that reads from a text file a few different ways. I'm just working on menu option #2 still (reading backwards from the end of the file), but I can't wrap my head around what I'm doing wrong. I've been at this for a few days now and just can't find any good resources to help on this. Any help would be appreciated.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  #include &lt;istream&gt;
  #include &lt;math.h&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  const int SIZE = 20;                     
  typedef char String[SIZE];
  
  //prototypes
  void Menu(int &amp;);
  void ReadFile(ifstream &amp;);
  void BackwardFile(ifstream &amp;,long &amp;);
  long CountFile(ifstream &amp;,long &amp;);
  
  int main()
  {  
      char filename[]= "grades.txt";
      int choice;
      long numBytes;
  
      ifstream InList;
      InList.open(filename);
  
      if(InList.good())
      {
          do
          {         
              Menu(choice); 
              switch(choice)
              {
                  case 1:  
                      ReadFile(InList);
                      break;
                  case 2:
                      CountFile(InList,numBytes);
                      BackwardFile(InList,numBytes);
                      break;
                  case 3:
                      cout &lt;&lt; "Pick a start position between 0 - " &lt;&lt; numBytes &lt;&lt; endl;
                      break;
                  /*case 4:*/
  
                  case 5:
                      cout &lt;&lt; "\n GOOD BYE." &lt;&lt; endl &lt;&lt; endl; 
                      break;
              }
  
          }while (choice != 5);                         
      }
      else
          cout &lt;&lt; "File did not open successfully." &lt;&lt; endl &lt;&lt; endl;
  
      system("PAUSE");
      return EXIT_SUCCESS;
  }
  
  void Menu(int &amp;choice) 
  { 
      cout &lt;&lt; "\n    Choose an option:";
      cout &lt;&lt; "\n...................................";
      cout &lt;&lt; "\n 1- Display All Contents of the File";
      cout &lt;&lt; "\n 2- Display Content in Reverse Order";
      cout &lt;&lt; "\n 3- Display from Point A to Point B";
      cout &lt;&lt; "\n 4- Display from Point B to Point A";
      cout &lt;&lt; "\n 5- Exit";
      cout &lt;&lt; "\n\n Enter your choice: ";
      cin &gt;&gt; choice; 
  } 
  
  void ReadFile(ifstream&amp; inFile)
  {
      char byte;
  
      inFile.clear();
  
      cout&lt;&lt; "\nReading contents from the file:" &lt;&lt;endl;
      if(inFile)
      {
          inFile.get(byte);
          while(inFile.good())
          {
              cout &lt;&lt; byte;
              inFile.get(byte);  
          }
      }
      inFile.close();
  }
  
  void BackwardFile(ifstream&amp; inFile, long&amp; numBytes)
  {
      char byte;
  
      inFile.clear();
      cout&lt;&lt; "\nReading contents backwards from the file:" &lt;&lt;endl;
  
      inFile.seekg(numBytes, ios::end);
  
      while(inFile)
      {
          inFile.get(byte);               
          cout &lt;&lt; byte;
          numBytes--;
          inFile.seekg(numBytes);  
      }
      inFile.close();
  }                              
  
  long CountFile(ifstream&amp; inFile, long&amp; numBytes)                           
  {
      inFile.seekg(0L, ios::end);
      numBytes = inFile.tellg();
      return numBytes;
  }
  </code></pre>