File Truncation
File Truncation
Does anybody know how to truncate files, at least under Windows, and other OS'es, just like HIEW (hex editor application) does?
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
I noticed how many questions you asked that can be answered with a forum search or google. While I don't know the answer to this one, or where to find the answer, may i kindly suggest you to have a good look and give us some indication of what you have tried.
By the appearance of the question, you might want to reverse-engineer the algorithm involved. Which, probably, is the most educational way to understanding it.
By the appearance of the question, you might want to reverse-engineer the algorithm involved. Which, probably, is the most educational way to understanding it.
I try to ask for small details that even when finding information in Google, are hard to figure out and need to be extensively reworked. After that, I try to build up something to finish the issue at once. For example the CHS to LBA to CHS question, I didn't know that actually the HIW document about it could completely answer my question, yet I already knew its existence long ago.I noticed how many questions you asked that can be answered with a forum search or google.
-------------------
Basically one of the things HIEW can do is to shred a file, for example from the half of it and leave only the first 50%, and it does it immediately.
The things I have tried are horribly inefficient, like recopying the file into a new one up to the offset it should be preserved and then deleting the original and finally rename the new one.
What I would do, if I could, were to truncate the file by indicating the EOF in the "file allocation table" to make it smaller and thus effectively cause a truncation effect. Maybe there's some API to do so, I have looked for it for like 2 years, but maybe I should look again...
In Unix if you just wanna truncate, you can do:
Either O_WRONLY or O_RDWR will do. For details, see open(2). On Windows the same can be done with a flag for CreateFile.
If your system doesn't support this, you can delete the file, and recreate it without writing anything into it.
As for actually saving the beginning (and just dumping out the rest) you need to look into system specific stuff. Technically it's done by simply setting the file-length in the filesystem to a smaller value, and freeing whatever blocks aren't part of the file anymore. It took me full 2 minutes to browse Win32 API reference to find out that there's an API function called SetEndOfFile... see said API documentation to figure out how it works.
Code: Select all
int fd = open("filename-to-truncate", O_WRONLY | O_TRUNC);
if(fd < 0) { /* error: */ exit(1); }
close(fd);
If your system doesn't support this, you can delete the file, and recreate it without writing anything into it.
As for actually saving the beginning (and just dumping out the rest) you need to look into system specific stuff. Technically it's done by simply setting the file-length in the filesystem to a smaller value, and freeing whatever blocks aren't part of the file anymore. It took me full 2 minutes to browse Win32 API reference to find out that there's an API function called SetEndOfFile... see said API documentation to figure out how it works.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.