troubleshooting Question

Remove data from file

Avatar of chsalvia
chsalvia asked on
C
13 Comments6 Solutions467 ViewsLast Modified:
I recently asked a similar question - but the issue has become more relevant to me as I continue working on my program, so I want to ask the question again in greater detail.

After reading over the GNU docs regarding file manipulation, there seems to be no facility for removing data from a file.  My program creates a file which stores various records in no particular order.  If I want my program to delete an arbitrary record, it seems my only option would be to recreate the entire file with one less record, and then delete the old file.  

But recreating the entire file seems like a very expensive operation, especially if the file is large.  Surely database programs which store thousands of records do not recreate the data file every time a single record is removed.  But the only other conceivable way to remove an arbitrary record from a data file would be to seek the record, and then move all subsequent records backwards by one record.  This is still a rather expensive operation, but I guess it's the only option with a disk-based data structure.  

But is it even possible to do this?  I realize there is no such facility defined in ANSI C, but I'm not worried so much about portability.  Still, I can't find anything mentioned in the GNU docs either.  So, what are some ways to remove data from a file without recreating the entire file on a GNU Linux platform?  
ASKER CERTIFIED SOLUTION
imladris

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 6 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 6 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros