Xref: utzoo comp.lang.c:33380 comp.os.msdos.programmer:1795 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jarthur!dfoster From: dfoster@jarthur.Claremont.EDU (Derek R. Foster) Newsgroups: comp.lang.c,comp.os.msdos.programmer Subject: Re: How do I SHORTEN a file without rewriting it? Keywords: truncation Message-ID: <9505@jarthur.Claremont.EDU> Date: 2 Nov 90 23:48:53 GMT References: <1162@bilver.UUCP> <747@csource.oz.au> Followup-To: comp.lang.c Organization: Harvey Mudd College, Claremont, CA 91711 Lines: 30 In article <747@csource.oz.au> david@csource.oz.au (david nugent) writes: >In <1162@bilver.UUCP> alex@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