[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?
Resizing a binary file?
Let's say I have a binary file of 3 integers:
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):
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);
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.
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.
To daerid: lol[lol], your solution makes me laugh. Indeed, if I were the first poster, I did that as well.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement