Как удалить запись из файла - PullRequest
1 голос
/ 23 апреля 2020

Я работаю над кодом, который создает двоичный файл, который содержит структуры данных студентов. Одна из вещей, которые мне нужно сделать, это удалить запись из файла, но я не знаю, как это сделать. Я не уверен, как бы я go об идентификации записи на основе идентификационного номера, а затем удалить этот файл. Буду признателен за любую помощь, совет или инструкции о том, что делать.

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <stdio.h>  
#include <string.h> 

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 << "Choose an option on what to do."<< endl;
    cout << "Option 1: Create a Binary File." << endl;
    cout << "Option 2: Check if a file exist." << endl;
    cout << "Option 3: Populate the records of the file." << endl;
    cout << "Option 4: Display contents of the file." << endl;
    cout << "Option 5: Append a record to the file." << endl;
    cout << "Option 6: Delete a record from the file." << endl;
    cout << "Option 7: Search for a record in the file." << endl;
    cout << "Option 8: Modify a record from the file." << endl;
    cout << "Option 9: Sort the file." << endl;
    cout << "Option 10: Wipe a file." << endl;
    cin >>  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 << "Binary File named output has been created."<< endl;
        }


    if (choice==2){
        file.open("output", ios::in | ios::binary);
        if (file.fail()) {
            cout << "File could not be opened." << endl;
        }
        else {
            cout << "The file is there" << endl;
        }
    }


    if (choice==3){
        file.open("output", ios::out | ios::binary);
        file.read(reinterpret_cast<char *>(&person), sizeof(person));
        cout << "Populating the record with information."<< endl;      
        cout << "Enter the following data about a person " << endl;
        cout << "First Name: "<< endl;
        cin.getline(person.fname, NAME_SIZE);
        cout << "ID Number: "<< endl;
        cin >> person.id;
        file.write(reinterpret_cast<char *>(&person), sizeof(person));
        file.close();       
        }




    if (choice==4){

        fstream MyFile("output",ios::binary|ios::out);
        Student person[3];
        for(int i = 0; i < 3; i++)
        MyFile.read((char *) &person[i], sizeof(Student));
        MyFile.close();
        }


    if (choice==5){
        int i;
        char fname;
        int id;
        fstream file;
        file.read(reinterpret_cast<char *>(&person), sizeof(person));
        cout << "How many records do you want to append?" << endl;
        cin >> number;
        file.open("output",ios::out|ios::app|ios::binary);
        file.write(reinterpret_cast<char *>(&person), sizeof(person));
        for(i=1; i<=number; i++)
        {
            cout<<"Enter the name:  "<< endl;
            cin >> fname;
            cout<<"Enter ID :  "<<endl;
            cin >> id;
            file<<fname<<endl;   
            file<<id<<endl;
        }
        file.close( );

    }


    if (choice==6){

}



    if (choice==7){
    }


    if (choice==8){
    }


    if (choice==9){
    }


    if (choice==10){
        char answer;
        cout << "Are you sure you want to wipe the file? 1 for Yes or 2 for No" << endl;
        cin >> answer;
        if (answer==1){
            std::ofstream ofs;
            ofs.open("output", std::ofstream::out | std::ofstream::trunc|ios::binary);
            cout << "Output File has succesfully been deleted"<< endl;
            ofs.close();

        }
        if (answer!=1)
            {
            cout << "File has not been deleted"<< endl;
        }
        }
    }
    ```


...