How do you truncate a file?

Brian Fitzgerald fitz at mml0.meche.rpi.edu
Wed May 8 14:13:45 AEST 1991


Lance Bledsoe writes:
>In article <f25g_zd at rpi.edu> fitz at mml0.meche.rpi.edu (Brian Fitzgerald) writes:
>>Rudi Cilibrasi writes:
>>>Suppose I have a 100-byte file called "myfile.dat", and I want to get
>>>rid of the last 10 bytes.
>>
>>#include <stdio.h>
>>main () {
>>int	i;
>>for (i=0; i < 90 ; i++) {
>>		(void) putchar(getchar());
>
>Do you think you could have thought of a *slower* way to do this???

Since the original poster's program uses fread and fwrite, it will run
much faster than mine does, especially if his fread and fwrite call a
decent assembler implementation of bcopy.

The speed point is well taken, but neither the buffered I/O functions
nor the character macros help Rudi, who would like to truncate his file
in place without reading it into memory or using a temporary file.
He's not running unix, and I think he's looking elsewhere for help
now.

Brian



More information about the Comp.lang.c mailing list