Mail Archives: djgpp/1994/12/13/04:50:10
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 -