• 13
• 18
• 19
• 27
• 9

# Resizing a binary file?

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

## 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 on other sites
It pretty much goes like this:

1. Open the file for read access
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 intsfp = fopen("filename","wb");fwrite(nums,sizeof(int),2,fp);fclose(fp);

##### 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 on other sites
There is: SetEndOfFile (). ^_^

To daerid: lol[lol], your solution makes me laugh. Indeed, if I were the first poster, I did that as well.