title: Deleting specific line from file in c++
body: <p>I want the user to enter the roll number of a student, and then inside my file I want to delete that specific record.</p>
  <p>Lets say my file contains the following information</p>
  <pre class="lang-none prettyprint-override"><code>1 Abhi 92
  2 Ravi 89
  3 Kiran 45
  </code></pre>
  <p>The user enters 2, then I want the file to change to:</p>
  <pre class="lang-none prettyprint-override"><code>1 Abhi 92
  3 Kiran 45
  </code></pre>
  <p>How is it possible?</p>
  <p><strong>EDIT</strong>:</p>
  <p>It is a part of this project, I want to create another case which says delete:</p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;fstream&gt;
  using namespace std;
  class Students
      {
          private:
  
              int roll_number;
              char name[80],dummy;
              float marks;
              char grade;
          public:
              void read_info()
              {
                  calc_grade();
                  cout&lt;&lt;roll_number&lt;&lt;&quot;\t&quot;&lt;&lt;name&lt;&lt;&quot;\t\t\t&quot;&lt;&lt;marks&lt;&lt;&quot;\t\t&quot;&lt;&lt;grade&lt;&lt;endl&lt;&lt;endl;
              }
              void write_info()
              {
                  cout&lt;&lt;&quot;Roll Number: &quot;;
                  cin&gt;&gt;roll_number;
                  cin.get(dummy);
                  cout&lt;&lt;&quot;Name: &quot;;
                  cin.get(name,80);
                  cin.get(dummy);
                  start:
                  cout&lt;&lt;&quot;Marks: &quot;;
                  cin&gt;&gt;marks;
                  if(marks &lt; 0 || marks &gt; 100)
                  {
                      cout&lt;&lt;&quot;Invalid&quot;&lt;&lt;endl;
                      goto start;
                  }
                  cin.get(dummy);
              }
              void calc_grade()
              {
                  if(marks&gt;=80)
                      grade = 'A';
                  else if(marks&gt;=60)
                      grade = 'B';
                  else if(marks&gt;=40)
                      grade = 'C';
                  else if(marks&gt;=20)
                      grade = 'D';
              }
              int return_roll()
              {
                  return roll_number;
              }
              float return_marks()
              {
                  return marks;
              }
      };
  int main()
      {
          int start_swit;
          do
          {
              system(&quot;pause&quot;);
              system(&quot;cls&quot;);
              cout&lt;&lt;&quot;\t\t\t\t\tStudents Database&quot;&lt;&lt;endl;
              cout&lt;&lt;&quot;------------------------------------------------------------------------------------------------------------------------&quot;&lt;&lt;endl;
              cout&lt;&lt;&quot;------------------------------------------------------------------------------------------------------------------------&quot;&lt;&lt;endl;
              Students s;
              fstream fil;
              char ch;
              int search_rn;
              cout&lt;&lt;&quot;1.Read\t\t2.Write\t\t3.Specific student\t\t4.Topper&quot;&lt;&lt;endl&lt;&lt;endl;
              cin&gt;&gt;start_swit;
              switch(start_swit)
                  {
                  case 1:
                      {
                          system(&quot;cls&quot;);
                          fil.open(&quot;studentdata.bin&quot;,ios::in|ios::binary);
                          cout&lt;&lt;&quot;Roll.N\t  Name\t\t\tMarks\t\tGrade&quot;&lt;&lt;endl;
                          cout&lt;&lt;&quot;-----------------------------------------------------&quot;&lt;&lt;endl;
                          while(fil.read((char*)&amp;s,sizeof(s)))
                          {
                              s.read_info();
                          }
                          fil.close();
                          break;
                      }
                  case 2:
                      {
                          system(&quot;cls&quot;);
                          fil.open(&quot;studentdata.bin&quot;,ios::in|ios::out|ios::binary|ios::app);
                          ch = 'y';
                          do{
                              s.write_info();
                              fil.write((char*)&amp;s,sizeof(s));
                              cout&lt;&lt;&quot;Do you want to enter more records?: &quot;;
                              cin&gt;&gt;ch;
                          }
                          while(ch == 'y');
                          fil.close();
                          break;
                      }
                  case 3:
                      {
                          int found = 0;
                          fil.open(&quot;studentdata.bin&quot;,ios::in|ios::binary);
                          cout&lt;&lt;&quot;Enter the roll number of the student: &quot;;
                          cin&gt;&gt;search_rn;
                          while(fil.read((char*)&amp;s,sizeof(s)))
                                {
                                    if(search_rn == s.return_roll())
                                    {
                                        cout&lt;&lt;&quot;Roll.N\t\tName\t\tMarks\t\tGrade&quot;&lt;&lt;endl;
                                        cout&lt;&lt;&quot;-----------------------------------------------------&quot;&lt;&lt;endl;
                                        s.read_info();
                                        found = 1;
                                        break;
                                    }
                                }
                          if(found != 1)
                              cout&lt;&lt;&quot;Student not found&quot;&lt;&lt;endl;
                          fil.close();
                          break;
                      }
                  case 4:
                      {
                          float highest_marks = 0;
                          int roll_high;
                          fil.open(&quot;studentdata.bin&quot;,ios::in|ios::binary);
                          while(fil.read((char*)&amp;s,sizeof(s)))
                              {
                                  if(s.return_marks()&gt;= highest_marks)
                                      {
                                              highest_marks = s.return_marks();
                                              roll_high = s.return_roll();
                                      }
                              }
                          fil.close();
                          fil.open(&quot;studentdata.bin&quot;,ios::in|ios::binary);
                          while(fil.read((char*)&amp;s,sizeof(s)))
                                {
                                    if(roll_high == s.return_roll())
                                    {
                                        cout&lt;&lt;&quot;Roll.N\t\tName\t\tMarks\t\tGrade&quot;&lt;&lt;endl;
                                        cout&lt;&lt;&quot;-----------------------------------------------------&quot;&lt;&lt;endl;
                                        s.read_info();
                                        break;
                                    }
                                }
                          fil.close();
                          break;
                      }
  
                  }//switch end
              }// Do while loop end
              while(start_swit&gt;=1 &amp;&amp; start_swit&lt;=4);
          return 0;
      }// main() end
  </code></pre>