How do I SHORTEN a file without rewriting it?

Tiny Bubbles... ho at hoss.unl.edu
Mon Nov 5 08:51:29 AEST 1990


In <9505 at jarthur.Claremont.EDU> dfoster at jarthur.Claremont.EDU (Derek R. Foster) writes:
>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
>>Write zero bytes at that position.
>                                              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...

If I were doing it, I'd just flush the stream, use the fileno() macro
to get the handle number, and then either close it or fseek() to the 
beginning (just to be safe).

I often do crappy things like changing file modes, etc. in this manner,
and it works fine.  Just flush before you access with handles.  Again,
I'm paranoid.  Most of the time, the flush and seek aren't needed.
--
        ... Michael Ho, University of Nebraska
Internet: ho at hoss.unl.edu | "Mine... is the last voice that you will ever hear."



More information about the Comp.lang.c mailing list