title: Reading and writing to .dat file
body: <p>Why does this file always contain an extra <code>Fraction</code> structure? If a user enters option 3 to view all fractions when the program just starts (the file should be empty), <code>0/0</code> is printed out.  If a user selects option one and enters a fraction <code>1/1</code>, and then selects option 3, two fractions are printed out <code>1/1 1/1</code>.</p>
  
  <p>Note: to run this you will need to create an empty <code>.dat</code> file called  <code>"fractions.dat"</code></p>
  
  <p>I really appreciate your help!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  struct Fraction
  {
      int num, den;
  };
  
  int search(fstream&amp; file, Fraction* f);
  int menu();
  void proccessChoice(int i);
  Fraction* readFrac();
  
  Fraction* fracs[100];
  int index;
  string fileName = "fractions.dat";
  fstream file(fileName,  ios::in | ios::out | ios::binary | ios::trunc);
  
  //structure called Fraction with num and den as its only integer members
  int main()
  {
      if (!file)
      {
          cout &lt;&lt; "Error opening file. Program aborting.\n";
          system("pause");
          exit(1);
      }
      int choice;
      do
      {
          choice = menu();
          proccessChoice(choice);
      } while(choice != 4);
  
  
      system("pause");
      return 0;
  }
  
  
  
  int menu()
  {
      int c;
      cout &lt;&lt; "1.Enter new fraction" &lt;&lt; endl;
      cout &lt;&lt; "2.Display/update a fraction" &lt;&lt; endl;
      cout &lt;&lt; "3.Display all fractions" &lt;&lt; endl;
      cout &lt;&lt; "4.Quit" &lt;&lt; endl;
      cin &gt;&gt; c;
      return c;
  }
  
  void proccessChoice(int i)
  {
      switch(i)
      {
      case 1:
          {
              cout &lt;&lt; "Enter a fraction to add: ";
              fracs[index] = readFrac();
              file.seekp(0, ios::end);
              file.write(reinterpret_cast&lt;char*&gt;(fracs[index]), sizeof(Fraction));
              /*cout &lt;&lt; fracs[index]-&gt;num &lt;&lt; "/" &lt;&lt; fracs[index]-&gt;den ;*/
              index++;
          }
              break;
      case 2:
          {
              cout &lt;&lt; "Enter fraction to view/update: ";
              Fraction* fToFind = readFrac();
              int i = search(file, fToFind);
              if (i == -1)
              {
                  cout &lt;&lt; "Fraction not found." &lt;&lt; endl;
              }
              else
              {
                  char r;
                  cout &lt;&lt; fToFind-&gt;num &lt;&lt; "/" &lt;&lt; fToFind-&gt;den &lt;&lt; " found at index " &lt;&lt; i &lt;&lt; ". Update? [y/n]";
                  cin &gt;&gt; r;
                  if(r == 'y' || r == 'Y')
                  {
                      cout &lt;&lt; "Enter a new fraction: ";
                      Fraction* fToWrite = readFrac();
                      file.seekp(sizeof(Fraction) * (i));
                      file.write(reinterpret_cast&lt;char*&gt;(fToWrite), sizeof(Fraction));
                  }
              }
              cout &lt;&lt; i &lt;&lt; endl;
              cout &lt;&lt; "Case 2" &lt;&lt; endl;
          }
              break;
      case 3:
          {
              Fraction* cur = new Fraction();
              //int pos = -1;
              if (!file)
              {
                  cout &lt;&lt; "Error opening file. Program aborting.\n";
                  system("pause");
                  exit(1);
              }
              file.seekg(0, ios::beg);
              while(!file.eof())
              {
                  file.read(reinterpret_cast&lt;char *&gt;(cur), sizeof(Fraction));
                  cout &lt;&lt; cur-&gt;num &lt;&lt; "/" &lt;&lt; cur-&gt;den &lt;&lt; endl;
              }
                      cout &lt;&lt; "Case 3" &lt;&lt; endl;
          }
              break;
      }
  }
  
  int search(fstream&amp; file, Fraction* f)
  {
      Fraction* cur = new Fraction();
      int pos = -1;
      if (!file)
      {
          cout &lt;&lt; "Error opening file. Program aborting.\n";
          system("pause");
          exit(1);
      }
      file.seekg(0, ios::beg);
      while(!file.eof())
      {
          file.read(reinterpret_cast&lt;char *&gt;(cur), sizeof(Fraction));
          if((cur-&gt;num == f-&gt;num) &amp;&amp; (cur-&gt;den == f-&gt;den))
          {
              cout &lt;&lt; "\nFrac found!" &lt;&lt; cur-&gt;num &lt;&lt; "/" &lt;&lt; cur-&gt;den&lt;&lt;endl;
              pos = file.tellg()/sizeof(Fraction) - 1;
              return pos;
          }
      }
      return pos;
  }
  
  Fraction* readFrac()
  {
      Fraction* f = new Fraction();
      char slash;
      cin &gt;&gt; f-&gt;num &gt;&gt; slash &gt;&gt; f-&gt;den;
      return f;
  }
  </code></pre>
  
  <p>Attempted solution 1: works for the first attempt of the user to view all fractions but on the second attempt the file is not found.</p>
  
  <pre><code>case 3:
          {
              Fraction* cur = new Fraction();
              //int pos = -1;
              if (!file)
              {
                  cout &lt;&lt; "Error opening file. Program aborting.\n";
                  system("pause");
                  exit(1);
              }
              file.seekg(0, ios::beg);
              while(!file.eof())
              {
                  if(file.read(reinterpret_cast&lt;char *&gt;(cur), sizeof(Fraction)).gcount()== sizeof(Fraction))
                  {
                      cout &lt;&lt; cur-&gt;num &lt;&lt; "/" &lt;&lt; cur-&gt;den &lt;&lt; endl;
                  }
              }
                      cout &lt;&lt; "Case 3" &lt;&lt; endl;
          }
              break;
      }
  </code></pre>