File Truncation

Programming, for all ages and all languages.
Post Reply
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

File Truncation

Post by ~ »

Does anybody know how to truncate files, at least under Windows, and other OS'es, just like HIEW (hex editor application) does?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Possibly, if you would care to tell us what HIEW does?
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
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:

Post by Combuster »

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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Post by ~ »

I noticed how many questions you asked that can be answered with a forum search or google.
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.

-------------------


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...
ehird
Member
Member
Posts: 214
Joined: Thu Mar 15, 2007 8:48 am

Post by ehird »

char *blah = malloc((lengthoffile/2) * sizeof(char));
fgets(myfile, lengthoffile/2, blah);
fwrite(newfile, blah);

?
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

In Unix if you just wanna truncate, you can do:

Code: Select all

  int fd = open("filename-to-truncate", O_WRONLY | O_TRUNC);
  if(fd < 0) { /* error: */ exit(1); }
  close(fd);
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.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

For a Unix OS try truncate/ftruncate, for windose try what mystran segested.
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

Oh ok, I didn't actually know of truncate(2) :)
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

I knew it existed on unix, I just did what ~ should of done in the first place, googled for "set file size".

But I didn' know about SetEndOfFile though.
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
Post Reply