修改数据文件中的记录

时间:2019-08-24 18:20:26

标签: c++ codeblocks

我正在创建一个程序来修改二进制文件中的记录。但是我总是最终创建两个相似的记录。

我将所有数据复制到另一个文件。如果我发现要修改的记录,则读取修改后的数据并将其复制到新文件中。复制所有记录后,重命名文件

课程:

class student
{
        char name[200];
        int rno;
        float marks;
        char grad;
        void grade()
        {
            if(marks < 33)
                grad = 'F';
            else if(marks < 50)
                grad = 'D';
            else if(marks < 60)
                grad = 'C';
            else if(marks < 80)
                grad = 'B';
            else
                grad = 'A';
        }
    public:
        void get()
        {
            fflush(stdin);
            cout << "Name: ";
            gets(name);
            cout << "Roll number: ";
            cin >> rno;
            cout << "Marks:" ;
            cin >> marks;
            grade();
        }
        ///////////////////////////////////////////////////////////
        void showname()
        {
            cout << "Name: " << name << endl;
        }
        ///////////////////////////////////////////////////////////
        void show()
        {
            cout << "Name: " << name << endl;
            cout << "Roll number: " << rno << endl;
            cout << "Marks: " << marks << endl;
            cout << "Grade: " << grad << endl;
        }
        ///////////////////////////////////////////////////////////
        int get_rno()
        {
            return rno;
        }
        ////////////////////////////////////////////////////////////
        float get_marks()
        {
            return marks;
        }
        ////////////////////////////////////////////////////////////
        char get_grade()
        {
            return grad;
        }
        ////////////////////////////////////////////////////////////
        char* getname()
        {
            return name;
        }
};

修改功能:

void modify_rec()
{
    int rno;
    ifstream fin("stu.dat",ios::binary);
    ofstream fout("modif.dat",ios::binary);
    student s;
    cout << "Enter the roll number whose details you want to edit:";
    cin >> rno;
    while(!fin.eof())
    {
        if(fin.eof())
            break;
        fin.read((char*)&s, sizeof(s));
        if(s.get_rno() == rno)
        {
            s.get();
            fout.write((char*)&s, sizeof(s));
            rno = -1;
        }
        else
        {
            fout.write((char*)&s, sizeof(s));
        }
    }
    fin.close();
    fout.close();
    remove("stu.dat");
    rename("modif.dat","stu.dat");
}

主要功能:

int main()
{
    char op;
    int ch;
    do
    {
        system("CLS");
        cout << "Select an option:" << endl;
        cout << "\t1.)Add record." << endl;
        cout << "\t2.)Display all." << endl;
        cout << "\t3.)Search by roll number." << endl;
        cout << "\t4.)Search by name" << endl;
        cout << "\t5.)Modify record." << endl;
        cout << "\t6.)Delete record." << endl;
        cout << "\t7.)Display list of distinction holders." << endl;
        cout << "\t8.)Create list of failures." << endl;
        cout << "\t9.)Display report card." << endl;
        cin >> ch;
        switch(ch)
        {
            case 1: add_rec(); break;
            case 2: disp_all(); break;
            case 3: search_rno(); break;
            case 4: search_name(); break;
            case 5: modify_rec(); break;
            case 6: delete_rec(); break;
            case 7: disp_distinction(); break;
            case 8: create_failure(); break;
            case 9: disp_repcrd(); break;
        }
        cout << "Do you want to continue?" << endl;
        cin >> op;
    }while(tolower(op) == 'y');
}

0 个答案:

没有答案