How do I SHORTEN a file without rewriting it?

Derek R. Foster dfoster at jarthur.Claremont.EDU
Sat Nov 3 10:48:53 AEST 1990


In article <747 at csource.oz.au> david at csource.oz.au (david nugent) writes:
>In <1162 at bilver.UUCP> alex at bilver.UUCP (Alex Matulich) writes:
>
>>Is there a way to shorten a file, that is, chop some data off the end of
>>it, so that it doesn't consume as much physical space on the disk?  The
>>file I have is too big to read into memory and write back out again, and
>>there is not enough room on the disk to write out a temporary file.
>
>Write zero bytes at that position.

If this works, it isn't documented in the Microsoft C manuals I have.
(And believe me, I searched!) After SEVERAL calls to Microsoft,
(Two seperate people told me it couldn't be done from either within C or
through DOS! I thought these people were supposed to be knowledgeable!)
and a great deal of loud cursing, I was finally led to the chsize()
function. This seems to be the only way of doing this from within
Microsoft C, (And I suspect Turbo C as well.) If you are using
streams, you will probably have to close your stream, reopen the file
using handles, chsize() it, close it again, reopen using streams...
What a mess. But it works, and is better than (in my case) copying
a 20-meg file to a shorter length...

>Some C libraries have a chsize() function which does exactly that.
>Since those libraries also don't seem to allow writing of zero bytes
>you will need to create your own write function.

I'm not sure why it is preferable to create one's own write function
instead of just using chsize(). What is the advantage?

Derek Riippa Foster



More information about the Comp.lang.c mailing list