How do I SHORTEN a file without rewriting it?

Rahul Dhesi dhesi%cirrusl at oliveb.ATC.olivetti.com
Mon Nov 5 18:13:36 AEST 1990


In <9505 at jarthur.Claremont.EDU> dfoster at jarthur.Claremont.EDU (Derek R.
Foster) writes:

>>>Is there a way to shorten a file...

>>Write zero bytes at that position.

>If this works, it isn't documented in the Microsoft C manuals I have.
>[chsize()] seems to be the only way of doing this from within
>Microsoft C...

A write of zero bytes using the MS-DOS "write to file handle" system
call will indeed truncate the file at the current seek position.
User programs could well contain statements of the sort:

     count = read(fin, buf, count);
     if (count != -1)
          (void) write(fout, buf, count);

A zero-byte write could truncate the file connected with fout.  If we
had seeked to near the beginning of a long file and were trying to
overwrite some of the data without losing the rest, truncation would be
disastrous.

So it's at least an even bet that your C library carefully avoids
passing zero-byte writes on to MS-DOS.

The chsize() function, on the other hand, most likely simply does a
seek followed by zero-byte write.
--
Rahul Dhesi <dhesi%cirrusl at oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi
A pointer is not an address.  It is a way of finding an address. -- me



More information about the Comp.lang.c mailing list