0

I am creating a program to modify the records in a binary file. But i always end up creating two similar records.

I copy all the data to another file. if I find the record to be modified, I read the modified data and copy it to the new file. When all the records are copied, The remporary file is renamed

The class:

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;
        }
};

The modify function:

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");
}

Main function:

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');
}

  • Your while loop could be a problem. Read through https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons for some advice about how to structure it correctly. – Retired Ninja Aug 24 '19 at 18:28
  • @RetiredNinja thats exactly why I put `if(!fin.eof()) break; ` in the code – Dhruv Tyagi Aug 24 '19 at 18:46
  • That's great, but it is completely in the wrong place. Doing it the right way and checking if the read succeed is less code and makes more sense when you read it. Win win. – Retired Ninja Aug 24 '19 at 19:04
  • @RetiredNinja Ohh, thanks for pointing that out! I missed that its wrongly placed.... – Dhruv Tyagi Aug 24 '19 at 21:58

0 Answers0