Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to erase part of file in C++

Tags:

c++

file

stream

I wonder which is the fastest way to erase part of a file in c++.

I know the way of write a second file and skip the part you want. But i think is slow when you work with big files.

What about database system, how they remove records so fast?

like image 896
Sinjuice Avatar asked Dec 06 '25 01:12

Sinjuice


2 Answers

A database keeps an index, with metadata listing which parts of the file are valid and which aren't. To delete data, just the index is updated to mark that section invalid, and the main file content doesn't have to be changed at all.

like image 131
Ben Voigt Avatar answered Dec 07 '25 16:12

Ben Voigt


Database systems typically just mark deleted records as deleted, without physically recovering the unused space. They may later reuse the space occupied by deleted records. That's why they can delete parts of a database quickly.

The ability to quickly delete a portion of a file depends on the portion of the file you wish to delete. If the portion of the file that you are deleting is at the end of the file, you can simply truncate the file, using OS calls.

Deleting a portion of a file from the middle is potentially time consuming. Your choice is to either move the remainder of the file forward, or to copy the entire file to a new location, skipping the deleted portion. Either way could be time consuming for a large file.

like image 37
Kluge Avatar answered Dec 07 '25 16:12

Kluge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!