Sign in to follow this  

Resizing a binary file?

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

This topic is 4383 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this