Unable to delete a record from a c binary program I created
<p>thanks for your anticipated help.  I have been looking at this code for hours and days but I just cant seem to find out why my delRecFunc() isn't deleting just one record at a time.  It appears it is deleting the whole file.  </p>

<pre><code>    #include &lt;iostream&gt;
    #include &lt;fstream&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;string&gt;
    #include &lt;iomanip&gt;


 using namespace std;

 const int NAME_SIZE = 40, ADDR_SIZE = 50, PHONE_SIZE = 14;
char y, Y;
struct Info
 {
 char name[NAME_SIZE];
 int age;
 char address1[ADDR_SIZE];
 char phone[PHONE_SIZE];

   }people;

 void popFunc();
 void dispFunc();
 void appFunc();
 void delRecFunc();
 void searchFunc();
 void modRecFunc();
 int getNumRec();
 int validateRecord (int);
 void wipeFunc();
int main()
 {
 Info person;
char choice, option;
cout &lt;&lt; " The file we are working with is named stuff.dat,\n"
     &lt;&lt; "I will let you know if it unavailable "  &lt;&lt; endl;
 cout &lt;&lt; "  "&lt;&lt; endl;
do
{



 cout &lt;&lt; "  \n \n Simple People Records...  \n \n \n "
     &lt;&lt; "     Please choose from the following options: \n \n \n"
     &lt;&lt; " 1.  POPULATE THE BINARY FILE \n"
     &lt;&lt; " 2.  DISPLAY THE CONTENTS OF THE FILE\n"
     &lt;&lt; " 3.  APPEND A RECORD TO THE FILE\n"
     &lt;&lt; " 4.  DELETE A RECORD FROM THE FILE\n"
     &lt;&lt; " 5.  SEARCH FOR A CERTAIN RECORD\n"
     &lt;&lt; " 6.  MODIFY A CERTAIN RECORD. \n"
     &lt;&lt; " 7.  WIPE OUT THE ENTIRE CONTENTS OF THE FILE. \n" &lt;&lt;endl;
cin  &gt;&gt; option;

 switch(option)
 {
               case '1': popFunc();
                        system("pause");
                         break;

              case '2': dispFunc();
                      break;

              case '3': appFunc();
                      break;

              case '4': delRecFunc();
                      break;

              case '5':searchFunc();
                     break;

              case '6':modRecFunc();
                     break;

              case '7':wipeFunc();
                     break;



              }
}
  while(choice!= 1000);

 system("pause");
 return(0);
 }





void popFunc()

 {    fstream file("stuff.dat", ios::out | ios::binary);


 if (file.fail())
   {    
        cout &lt;&lt; "the file does not exist so we are creating it now...  ";
        system("pause");
        file.open("stuff.dat", ios::out);   

   }


cout &lt;&lt; "enter the following data about a person:  "&lt;&lt;endl;
cout &lt;&lt; "Name: "  &lt;&lt;endl;
cin &gt;&gt; ws;
cin.getline(people.name,NAME_SIZE);
cout &lt;&lt; "Age (integers only or the program will be corrupted):  " &lt;&lt;endl;
cin &gt;&gt; people.age;
cin.ignore(); //skip over the remaining newline.
cout&lt;&lt; "Address line 1:  ";
cin &gt;&gt; ws;
cin.getline(people.address1,ADDR_SIZE);
cout &lt;&lt; "Phone:  in the following format ie: 201.123.1234 (no hyphens)";
cin &gt;&gt; ws;
cin.getline(people.phone,PHONE_SIZE);
file.write(reinterpret_cast&lt;char *&gt;(&amp;people), sizeof(people));
file.close(); 
}

void dispFunc()

{

         ifstream file;
     file.open("stuff.dat", ios::binary);
         if (file.fail())
   {    
        cout &lt;&lt; "the file does not exist so we are creating it now...  ";
        system("pause");
        file.open("stuff.dat", ios::out);   

   }

     while(file.read(reinterpret_cast&lt;char *&gt; (&amp;people), sizeof(people)))
     {
     cout &lt;&lt; "Name:  "&lt;&lt; people.name &lt;&lt;endl;
     cout &lt;&lt; "Age:  "&lt;&lt; people.age &lt;&lt;endl;
     cout &lt;&lt; "Address:  " &lt;&lt;people.address1 &lt;&lt;endl;
     cout &lt;&lt; "Phone #: " &lt;&lt;people.phone &lt;&lt; " \n\n"&lt;&lt;endl;
     }
     file.close();

     }

void appFunc()
 {    ofstream file;
     file.open("stuff.dat",  ios::binary | ios::app); 
         if (file.fail())
   {    
        cout &lt;&lt; "the file does not exist so we are creating it now...  ";
        system("pause");
        file.open("stuff.dat", ios::out);   

   }

cout &lt;&lt; "Name: "  &lt;&lt;endl;
cin &gt;&gt; ws;
cin.getline(people.name,NAME_SIZE);
cout &lt;&lt; "Age (integers only or the program will be corrupted):  " &lt;&lt;endl;
cin &gt;&gt; people.age;
cin.ignore(); //skip over the remaining newline.
cout&lt;&lt; "Address line 1:  ";
cin &gt;&gt; ws;
cin.getline(people.address1,ADDR_SIZE);
cout &lt;&lt; "Phone:  Phone:  in the following format ie: 201.123.1234 (no hyphens)";
cin &gt;&gt; ws;
cin.getline(people.phone,PHONE_SIZE);
    file.write(reinterpret_cast&lt;char *&gt;(&amp;people), sizeof(people));
    file.close();





     }


 void searchFunc()
 {
 int count =1;
 int answer;
 long recNum;
 int recCount = getNumRec();
 fstream search;
 search.open("stuff.dat", ios::in | ios:: binary);
 cout &lt;&lt; " Please enter 1 - " &lt;&lt;recCount ;
 cout &lt;&lt; "  and I will display the data /n"&lt;&lt;endl;
 cin &gt;&gt;recNum;

  while(recNum&lt;1 || recNum &gt; recCount)
 {
     cout&lt;&lt;"Please enter a number between 1 and "&lt;&lt;recCount&lt;&lt;": ";
     cin&gt;&gt;recNum;
 }   
  answer = validateRecord(recNum);
 }
 void modRecFunc()

{
 int count=1;
 int answer;//to hold choice from the user
 long recNum;//to hold a record number
 int recCount = getNumRec();//variable to hold how many record the file has     
 fstream search;
 //open file
 search.open("stuff.dat",ios::in|ios::out|ios::binary);
 cout&lt;&lt;"Which record do you wish to edit? ";
 cin&gt;&gt;recNum;//variable to store the record the user wish to delete
 //validation so the user is not allow to enter more numbers than the actual
 //size of the file
 while(recNum&lt;1||recNum&gt;recCount)
 {
     cout&lt;&lt;"Please enter a number between 1 and "&lt;&lt;recCount&lt;&lt;": ";
     cin&gt;&gt;recNum;
 }     
 //move pointer to desire position
 cout&lt;&lt;endl;
 answer= validateRecord(recNum);//make sure the record is the right one
 cout&lt;&lt;endl;
 if(answer==1)
 {  
    //get the new data
 cout &lt;&lt; "Name: "  &lt;&lt;endl;
 cin &gt;&gt; ws;
 cin.getline(people.name,NAME_SIZE);
 cout &lt;&lt; "Age (integers only or the program will be corrupted):  " &lt;&lt;endl;
 cin &gt;&gt; people.age;
 cin.ignore(); //skip over the remaining newline.
 cout&lt;&lt; "Address line 1:  ";
 cin &gt;&gt; ws;
   cin.getline(people.address1,ADDR_SIZE);
 cout &lt;&lt; "Phone:  Phone:  in the following format ie: 201.123.1234 (no hyphens)";
 cin &gt;&gt; ws;
 cin.getline(people.phone,PHONE_SIZE);
    search.seekp((recNum-1)*sizeof(people), ios::beg);
    search.write(reinterpret_cast&lt;char *&gt;(&amp;people), sizeof(people));
    }
    cout&lt;&lt;endl;
search.close();              
}

 void wipeFunc()


 {    char option;
     ofstream file;
     cout &lt;&lt; "This option will delete all the contents of the file "&lt;&lt;endl;
     cout &lt;&lt; " Are you sure you want to delete the file?  Y or N " &lt;&lt;endl;
     cin &gt;&gt; option;
           if (option == 'y' || option == 'Y')

              {
                      cout &lt;&lt; " Warning, if you hit Y, you will lose everything " &lt;&lt;endl;
                      cout &lt;&lt; " Y to confirm,  N to return to main menu" &lt;&lt;endl;
                      cin &gt;&gt; option;

                          if(option == 'y' || option == 'Y')
                             {
                                    file.open("stuff.dat", ios::out | ios::binary);
                                    file.close();
                                    }      


     }
     }



int validateRecord (int recNum)
{
 int answer;// variable to hold the answer from the user
 fstream validate;
 //open file
 validate.open("stuff.dat",ios::in|ios::out|ios::binary);
 validate.seekg((recNum-1)*sizeof(people));
 //read record from file
 validate.read(reinterpret_cast&lt;char*&gt;(&amp;people),sizeof(people));
      cout&lt;&lt;endl;

     cout &lt;&lt; "the name is "&lt;&lt; people.name &lt;&lt;endl;
     cout &lt;&lt; "the age is "&lt;&lt; people.age &lt;&lt;endl;
     cout &lt;&lt; "the address is" &lt;&lt;people.address1 &lt;&lt;endl;
     cout &lt;&lt; "the phone is Phone:  " &lt;&lt;people.phone &lt;&lt;endl;


 cout&lt;&lt;"Is this the file you want?\n"
       "1. yes\n"
       "2. no\n"
       "Choice: ";
 cin&gt;&gt;answer;
// validate answer to 1 or 2
 while(answer&lt;1||answer&gt;2)
 {
     cout&lt;&lt;"enter only 1 or 2: ";
     cin&gt;&gt;answer;
 }
 cout&lt;&lt;endl;
 validate.close();
 return answer;
}

int getNumRec()   
{
int count =0;
fstream file;  

file.open("stuff.dat", ios::in|ios::binary);


if (file.fail())
{

  cout &lt;&lt; "Error opening file. Program aborting.\n";
}

  file.read(reinterpret_cast&lt;char *&gt;(&amp;people),sizeof(people));


while (!file.eof())
{

   count++;


  file.read(reinterpret_cast&lt;char *&gt;(&amp;people),
           sizeof(people));
}

file.close();//close the file
return count;//return count the erase function
}



void delRecFunc()
{
 int  count=1;
 int answer;            //to hold choice from the user
 double recNum;//to hold the record number the user wishes to delete
 int recCount = getNumRec();//variable to hold how many record the file has     

 fstream erase;
 erase.open("stuff.dat", ios::in|ios::out|ios::binary);

 Info Temp;            //*****NEW TEMP STRUCT VAR*****//

      cout &lt;&lt; "Please enter the phone number of the person you want to delete in this 
     format 201.123.1111)";
     cin &gt;&gt; recNum;       

  //move pointer to desire position
  erase.seekg((recNum-1)*sizeof(people));
 //read record from file
 erase.read(reinterpret_cast&lt;char*&gt;(&amp;people),sizeof(people)); //read record from file
 //get answer from the validateRecord function
  answer = validateRecord (recNum);
  if(answer == 1)
  {                          
     ofstream copy;
     copy.open("stuff2.dat", ios::out | ios::binary);

     erase.seekg(0*sizeof(people));   // Move pointer to beginning of the file
     erase.read(reinterpret_cast&lt;char*&gt;(&amp;people),sizeof(people));

// BEGIN NEW CODE //

    // Keep looping till there are no more records in the customer.dat file
     while (!erase.eof())
     {                 
         // If the id that the user wants deleted doesnt match the current
         // records id, copy it to the temp directory
         if(people.phone != Temp.phone)                                     
         {
             // Write the record to copy.dat
             copy.write(reinterpret_cast&lt;char*&gt;(&amp;people),sizeof(Info)); 
         }

         // Read the next line from customer.dat
         erase.read(reinterpret_cast&lt;char*&gt;(&amp;people),sizeof(Info));             
      }

     // Close both files
     erase.close();
     copy.close();
  }    
  // Delete the Old customer.dat file
  remove("stuff.dat");

  // Rename the temporary holder to "customer.dat"
  rename("stuff2.dat","stuff.dat");

}
</code></pre>
