• Advertisement
Sign in to follow this  

Is it possible to update a resource with rewriting the entire file?

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a program that saves to and reads from a number of files during runtime, all stored in a custom resource file format. In order to add and update files in the resource, right now I'm reloading all the files into memory and resaving/compressing them. Is there a better, more efficient method where I can just add and remove files without rewriting the entire file from top to bottom?

Share this post


Link to post
Share on other sites
Advertisement
I don't know how your resource file works, but if it stores the file data near the top, and then a table of files at the bottom to add files you could just write the new data over the old table, and then rewrite the updated table to the bottom.
To remove files i'm not sure, I suppose you could just remove the reference to the data in table of files and leave the data, and then rewrite the entire file periodicly without the "deleted" data.

Share this post


Link to post
Share on other sites
I'm not sure if that would work for me (the removing). In my case there are files that will need updating for almost every save, and that's about the same, requiring removing the old file and then adding the new version. Seems like there'd pile up a lot of unnecessary data...until the periodic cleansing. But that would seem odd to see sudden decreases in the file size.

Share this post


Link to post
Share on other sites
There are a number of algorithms for this coming from virtual memory managers, the terms escape me right now. You can have a flexible system with a table in the file which gives offsets and lengths of all resources. If the resources never grow, then you can always overwrite them in-place by seeking to the offset and writing.

If your resources grow, then you have a table at the end with open spaces. When a resource grows it's moved to the end of the file or any available space big enough, and a blank space entry is recorded in the space table. Adjecent spaces are coelesced, and the next time you're moving a large resource you can move it into that space. The algorithm is not too complicated, and the actual amount of disk reads/writes is just the size of your resource plus the two entries in the table.

If this is too complicated, maybe pad the resources with some extra bytes so they can grow some amount, and when this limit is passed you rewrite/compress the file.

Tom

Share this post


Link to post
Share on other sites
Either of those methods sounds perfect for what I need. Thanks for the explanations. I'll try implementing the first for a start.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement