Ravinder Singh

Male
Friday, February 24, 1984
I am a software engineer by profession and programmer by passion. I love working on software projects which help make a positive impact on the society.

File operations using objects in C++ - Delete method in the data access class

Category: Programming Tutorials Date: Thursday, July 21, 2016

For the C++ training group, from our classroom discussions on 19th July, 2016

This is the code which you should practice in the add method in the BranchDA data access class.

static bool Delete(int branchId)
    {
        Branch *temp = new Branch();

        // Add the branch information to the file

        // Create object of the file handling class
        fstream fs;
        fstream fs1;

        // Create the connection
        // Open the connection

        fs.open ("BranchData.dat", ios::in | ios::out | ios::binary | ios::app);
        fs1.open ("BranchData_temp.dat", ios::in | ios::out | ios::binary | ios::app);

        // Read or write the data

        fs.seekp(0, ios::beg);
        fs1.seekp(0, ios::beg);

        while(fs.read((char*)temp, sizeof(Branch)))
        {
             if(temp->GetId() != branchId)
             {
                 fs1.write( (char*)temp, sizeof(Branch) );
             }
        }

        fs.close();
        fs1.close();

        remove("BranchData.dat");
        rename("BranchData_temp.dat","BranchData.dat");

        return true;
    }



Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp