delete file contents(modified)

Whole file gets deleted instead of reuqired data. File.dat is the original file with already entered data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
     else if(option==5)
    {
        int rno;
              fstream w1;fstream e1;
              e1.open("temp.dat",ios::in|ios::out|ios::binary);
              w1.open("file.dat",ios::in|ios::out|ios::binary);
              cout<<"\nEnter roll number to delete";cin>>rno;
              bool value=false;
              while(w1.read((char*)&s1,sizeof(student)))
              {
                  if(s1.rno==rno)
                      value=true;
                  else
                         e1.write((char*)&s1,sizeof(student));
              }
                                      e1.close();
              w1.close();
                             if(value==false)
                             {
                cout<<"\nNot found"<<endl;
                             }
                else
                {
                     remove("file.dat");
              rename("temp.dat","file.dat");
                }
    }
Topic archived. No new replies allowed.