Hello,
I've got the following code:
wxString path = filepath;
wxString newpath = filepath;
fstream f(path.Append("/tests/tests.bin"), ios::in | ios::binary);
fstream fnew(newpath.Append("/tests/temp.bin"), ios::out | ios::binary);
Test* transferTest = new Test();
int thisSize = sizeof(Test);
bool success = false;
bool toBeDeleted = false;
int testNo = 1;
if (f)
{
f.seekg(0, ios::beg);
fnew.seekp(0, ios::beg);
while (testNo <= noOfTests)
{
toBeDeleted = false;
for (int i = 0; i < noOfSelectedRows; i++)
{
int row = selectedRows[i];
if (testNo == row)
{
toBeDeleted = true;
}
}
if (!toBeDeleted)
{
f.read(reinterpret_cast<char *>(transferTest), thisSize);
fnew.write(reinterpret_cast<char *>(transferTest), thisSize);
fnew.seekp(testNo*thisSize, ios::beg);
}
f.seekg(testNo*thisSize, ios::beg);
testNo++;
}
success = true;
f.close();
fnew.close();
remove(path.c_str());
rename(newpath.c_str(), path.c_str());
}
delete transferTest;
Essentially, what I have is a binary file with objects of the class Test. Trough a list in my GUI I make it possible to delete objects from this bunch. And it works fine as long as I delete one or more objects at the end of the file. However if I try to delete an object in the middle of the bunch, there becomes a "hole" in the "temp.bin" file and it keeps on writing the rest of the objects perfectly fine. So the file gets the exact same size as before.
I've been staring at this for a while now and I really can't see why it doesn't work the way I want it to, so hopefully someone can help :)
Thanks in advance!