Resizing a binary file?

Started by
3 comments, last by Wavarian 18 years, 4 months ago
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?
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 intsfp = fopen("filename","wb");fwrite(nums,sizeof(int),2,fp);fclose(fp);
daerid@gmail.com
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.
There is: SetEndOfFile (). ^_^

To daerid: lol[lol], your solution makes me laugh. Indeed, if I were the first poster, I did that as well.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Oh my God! Thankyou!

Ratings given to the both of you (since my original post wasn't very clear).

This topic is closed to new replies.

Advertisement