title: How delete a record from a file
body: <p>I am working on a code that creates a binary file that holds structures of student data. One of the things i need to do is to delete a record from a file but I don't know how to do that. I'm not sure how I would go about identifying a record based on the ID number and then delete that file. I would appreciate any help, advice or instructions on what to do.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  #include &lt;stdio.h&gt;  
  #include &lt;string.h&gt; 
  
  using namespace std;
  
  const int NAME_SIZE = 5;
  
   struct Student{
      char fname[NAME_SIZE]; 
      char id; 
  
      };
  
  int main() {
      int choice;
      fstream file; 
      int number;
      Student person;
  
  
      cout &lt;&lt; "Choose an option on what to do."&lt;&lt; endl;
      cout &lt;&lt; "Option 1: Create a Binary File." &lt;&lt; endl;
      cout &lt;&lt; "Option 2: Check if a file exist." &lt;&lt; endl;
      cout &lt;&lt; "Option 3: Populate the records of the file." &lt;&lt; endl;
      cout &lt;&lt; "Option 4: Display contents of the file." &lt;&lt; endl;
      cout &lt;&lt; "Option 5: Append a record to the file." &lt;&lt; endl;
      cout &lt;&lt; "Option 6: Delete a record from the file." &lt;&lt; endl;
      cout &lt;&lt; "Option 7: Search for a record in the file." &lt;&lt; endl;
      cout &lt;&lt; "Option 8: Modify a record from the file." &lt;&lt; endl;
      cout &lt;&lt; "Option 9: Sort the file." &lt;&lt; endl;
      cout &lt;&lt; "Option 10: Wipe a file." &lt;&lt; endl;
      cin &gt;&gt;  choice;
  
      if (choice==1){
  
          char strdata[10000];
          file.open("output", ios::binary|ios::app|ios::out);
          if(file.is_open())
          {
          cin.getline(strdata, 10000);
          file.seekg(ios_base::end);
          file.write(strdata, strlen(strdata));
          }       
          file.close();
          cout &lt;&lt; "Binary File named output has been created."&lt;&lt; endl;
          }
  
  
      if (choice==2){
          file.open("output", ios::in | ios::binary);
          if (file.fail()) {
              cout &lt;&lt; "File could not be opened." &lt;&lt; endl;
          }
          else {
              cout &lt;&lt; "The file is there" &lt;&lt; endl;
          }
      }
  
  
      if (choice==3){
          file.open("output", ios::out | ios::binary);
          file.read(reinterpret_cast&lt;char *&gt;(&amp;person), sizeof(person));
          cout &lt;&lt; "Populating the record with information."&lt;&lt; endl;      
          cout &lt;&lt; "Enter the following data about a person " &lt;&lt; endl;
          cout &lt;&lt; "First Name: "&lt;&lt; endl;
          cin.getline(person.fname, NAME_SIZE);
          cout &lt;&lt; "ID Number: "&lt;&lt; endl;
          cin &gt;&gt; person.id;
          file.write(reinterpret_cast&lt;char *&gt;(&amp;person), sizeof(person));
          file.close();       
          }
  
  
  
  
      if (choice==4){
  
          fstream MyFile("output",ios::binary|ios::out);
          Student person[3];
          for(int i = 0; i &lt; 3; i++)
          MyFile.read((char *) &amp;person[i], sizeof(Student));
          MyFile.close();
          }
  
  
      if (choice==5){
          int i;
          char fname;
          int id;
          fstream file;
          file.read(reinterpret_cast&lt;char *&gt;(&amp;person), sizeof(person));
          cout &lt;&lt; "How many records do you want to append?" &lt;&lt; endl;
          cin &gt;&gt; number;
          file.open("output",ios::out|ios::app|ios::binary);
          file.write(reinterpret_cast&lt;char *&gt;(&amp;person), sizeof(person));
          for(i=1; i&lt;=number; i++)
          {
              cout&lt;&lt;"Enter the name:  "&lt;&lt; endl;
              cin &gt;&gt; fname;
              cout&lt;&lt;"Enter ID :  "&lt;&lt;endl;
              cin &gt;&gt; id;
              file&lt;&lt;fname&lt;&lt;endl;   
              file&lt;&lt;id&lt;&lt;endl;
          }
          file.close( );
  
      }
  
  
      if (choice==6){
  
  }
  
  
  
      if (choice==7){
      }
  
  
      if (choice==8){
      }
  
  
      if (choice==9){
      }
  
  
      if (choice==10){
          char answer;
          cout &lt;&lt; "Are you sure you want to wipe the file? 1 for Yes or 2 for No" &lt;&lt; endl;
          cin &gt;&gt; answer;
          if (answer==1){
              std::ofstream ofs;
              ofs.open("output", std::ofstream::out | std::ofstream::trunc|ios::binary);
              cout &lt;&lt; "Output File has succesfully been deleted"&lt;&lt; endl;
              ofs.close();
  
          }
          if (answer!=1)
              {
              cout &lt;&lt; "File has not been deleted"&lt;&lt; endl;
          }
          }
      }
      ```
  
  
  </code></pre>