delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/12/13/04:50:10

To: IBBT0 AT cc DOT uab DOT es
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: Help about truncating files
Date: Tue, 13 Dec 94 08:29:10 +0200
From: "Eli Zaretskii" <eliz AT is DOT elta DOT co DOT il>

You <IBBT0 AT cc DOT uab DOT es> wrote:
>> In other words, I am asking you for a instruction, if possible
>>  working in ANSI C and using streams (fopen(),...), not file
>> handles, to inform the operating system and the File Allocation
>> Table I want truncate the file.

dj AT stealth DOT ctron DOT com (DJ Delorie) replied
> DOS says that if you write zero bytes, the file is truncated where the
> pointer is.  With FILE* streams, this is tricky (because of the
> caching), but what you can do is this:
>
>	fflush(f);
>	lseek(fileno(f), POS, 0);
>	write(fileno(f), 0, 0);

mat AT ardi DOT com (Mat Hostetter) rightfully added:
> ftruncate() in the djgpp C library does exactly this, but without the
> fflush().

While all of the above is certainly true, I would like to point out
that it's inherently non-ANSI.  In fact, if memory serves, ANSI
explicitly says this can't be done in a portable way.  So I would
generally discourage such features in a program which should be
ANSI-compatible, or even just plain portable.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019