ReMaKe
ReMaKe

Reputation: 37

Deleting a record within a binary file?

I've been messing around with binary files the last whole week. One thing I can't figure out how to do is selectively delete a record from the binary file. Is this possible in c++? If not, what are ways in which you can do this.

I'm contemplating, maybe creating a duplicate file without the record inside, but that would seem rather inefficient for very large data files. Any more efficient ways, or any way of duplicating a file efficiently? I''m surprised there's ways to delete files, but not selectively delete from a file.

Upvotes: 0

Views: 986

Answers (2)

Soren
Soren

Reputation: 14688

Just make sure ever record have a flag "deleted" which you set to false when you first write the record, but then change to true if you want to mark the record deleted.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206607

Unlike data structures in memory, you can't pull out a record from a file. You have to:

  1. Read the contents of the file into an in-memory data structure
  2. Remove the relevant data from the data structure in-memory.
  3. Write the updated data structure back to the file.

Upvotes: 4

Related Questions