Search code examples
c++binaryfiles

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.


Solution

  • 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.