Jump to content
  • Advertisement
Sign in to follow this  
Wavarian

Resizing a binary file?

This topic is 4596 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

Let's say I have a binary file of 3 integers:
[0 | 1 | 2 | 3] [0 | 1 | 2 | 3] [0 | 1 | 2 | 3]
At the moment, the file size is set at 12 bytes. Now, I would like to "remove" the last integer (making the file size equal to 8 bytes), but I cannot find any function that would allow me to reposition the EOF marker. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
It pretty much goes like this:

1. Open the file for read access
2. Read file contents
3. Close the file
4. Process file contents (resizing if necessary)
5. Open the file for write access (over-writing the contents of the previous file if it exists, which in this case it does)
6. Write out the processed contents.
7. Close the file.

C Code might look like this (error checking omitted for brevity):


FILE* fp = fopen("filename","rb");
int nums[3];
fread(nums,sizeof(int),3,fp);
fclose(fp);
// don't need to modify the array, but only write out the first two ints
fp = fopen("filename","wb");
fwrite(nums,sizeof(int),2,fp);
fclose(fp);

Share this post


Link to post
Share on other sites
While that is a possible solution, the problem is that the file could have 100000 integers.

Another solution would be to create a temporary file, but I wanted to know if there was another way.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!