I'm currently learning how to play around with binary files.
However, the hardest hurdle i came across is deleting a record from the binary files. I read up a lot and many suggested creating a temp file and transfer it back to the original file without the record you want to delete.
Unfortunately, all the examples i stumbled upon uses classes to do that which i have yet to learn / allowed to use them
Hence, i tried to think of a way that deletes a record without the usage of classes.
Firstly, i had to open my current binary file and create a new `temp.dat` file.
1 2 3
|
afile.open(fileName, ios::in | ios::binary);
afile2.open("Temp.dat", ios::out | ios::binary);
|
Next, i'll have a `while loop` that reads all the information of my `fileName` and an `if-else` statement to see if the selected record `k` can be found before exchanging
1 2 3 4 5
|
while(afile.read(reinterpret_cast <char*>(&ai), sizeof(ai)))
{
if(ai != k) //unclear
afile2.write(reinterpret_cast<const char*>(&ai), sizeof(ai));
}
|
Followed by using the remove and rename function to swap both of them
1 2
|
remove(fileName);
rename("Temp.dat", "Menu.dat");
|
However, it seems to return me an infinite loop