dimanche 28 mai 2017

C++ Previous Record Gets deleted when writing a new object

**So i'm trying to save an Object from a class. When I add a new Record (Add a new object being saved) the previous records becomes deleted. what am I doing wrong? ** It seems that once i add a new object the one that was created previously gets deleted because idk. Been trying this for hours. can't seem to figure it out.

void Department2File(HANDLE screen, Department &Departments, int deptcount)
{
Department Testing;
fstream file;
file.open("Departmentsdata.dat", ios::out | ios::binary);
file.close();
file.open("Departmentsdata.dat", ios::in | ios::out | ios::binary);

if (!file)
{
    cout << "Error opening file.";

}

// writting data
long size = (deptcount * sizeof(Departments));

if (deptcount == 0)
{
    size = (deptcount * sizeof(Departments));
}
else
{
    size = (deptcount * sizeof(Departments))+1;
}

cout << "\nsaved!" << endl;;
file.clear();
file.seekp(size, ios::beg);
file.write(reinterpret_cast <char *>(&Departments), sizeof(Departments));
file.clear();
file.seekg(size, ios::beg);
file.read(reinterpret_cast<char *> (&Testing), sizeof(Departments));


cout << Testing.DepartmentID;////////////<--------------delete


///////////////////////////////
///////////////DELETE//////////
        file.clear();
        file.seekg(0, ios::beg);
        file.get(reinterpret_cast<char *> (&Testing), sizeof(Departments));
        cout <<"\n RECORD 0: "<<Testing.DepartmentID;

                                ////////////////////////////
file.close();

}

Aucun commentaire:

Enregistrer un commentaire