Truncate a File?

Barney Frank rose at galtee.cs.wisc.edu
Tue Jul 31 01:12:01 AEST 1990


I have a requirement to adjust the length of a data file.

1. Open the file for read and write.
2. Archive the earliest information in the file to another file system.
3. Move the latest information to the head of the file.
4. Ask the operating system to adjust the size of the file down to the new
	"high-water" mark.

  It is not sufficient to make a temporary copy of the section of
interest, unlink the original file, and rename the temporary, because
the size of the file is of the same order of that of the file system.
Specifically, what I need to do is

This sounds like a pretty elementary operation to me, but I'll be
damned if I can find a system call to do anything to the size of a
file other than just move it down to zero.  One can envision some
pretty complicated schemes involving reading and writing the FAT
directly, but such schemes lack portability and have the potential to
run afoul of various caching schemes.  Yet I know that it can be done,
because I am aware of at least one program that is able to pack a data
file in-place in this fashion.

Right now, I am thinking of falling back to a fixed file length
scheme- that is, to allocate a file as large as I can afford, never
adjust the size, and use an alternative data structure to keep track
of the high-water point.  I'd be very happy to hear any suggestions
that allow me to avoid this, though.  Thanks in advance.

	-Scott Rose
--
	Scott Rose
	rose at cs.wisc.edu



More information about the Comp.lang.c mailing list