In a file, each bit of information has a position. Removing some of those bits does not move the position of the other elements.
File
1111111111111111
222222222.....22
3333.33333.3.333
44.444.444.444.4
5555555555555555
becomes
1111111111111111
222222222.....22
44.444.444.444.4
5555555555555555
by removing
3333.33333.3.333
and moving
44.444.444.444.4
5555555555555555
into the location previous held by
3333.33333.3.333
44.444.444.444.4
So you can do it without a temporary file, buy using the following technique
- Open a random access file (we will be jumping around in it.
- Read the file until you detect the items to be deleted, keeping a "location" of the start of that area.
- Calculate the "size" of the deleted area.
- While there is area beyond the deleted area that hasn't been moved
- Copy that area over the deleted area.
- Reassign the deleted area to the area you just copied from.
- Write the end of the file.
Of course this is really, really dangerous; as any interruption in the procedure leaves you with a file that is not the original, nor the result. As files copies can easily be interrupted by power outages, killed programs, etc. you really don't want this approach as it is so hard to recover from a failure.
That's why the write a second file, wait till that's done and then move it over the original file is a far better solution.