1  void MainWindow::on_deleteButton_clicked()
2  {
3     QFile file("student.dat");
4     if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
5         QMessageBox::information(this, "Warning", "File not open");
6         return;
8     }
9     QDataStream ds(&file);
10    QFile tempFile("temp.dat");
11    if(!tempFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
12        QMessageBox::information(this, "Warning", "File not open");
13        return;
14    }
15    QDataStream temp(&tempFile);
16    int stNum = QInputDialog::getInt(this, "Student number", "Enter stNo :");
17    while(!file.atEnd()) {
18       ds >> st;
19       if (st.stNo != stNum)
20          temp << st;
21       else
22           QMessageBox::information(this, "Info", "Delete done!");
23    }
24    tempFile.flush();
25    file.close();
26    tempFile.close();
27    QFile::remove("student.dat");
28    QFile::rename("temp.dat", "student.dat");
29 }
