Xenix

T. William Wells bill at proxftl.UUCP
Thu Sep 15 15:08:49 AEST 1988


In article <171 at uniblab.UUCP> al at uniblab.UUCP (alan krantz) writes:
:   I'm writing a C program using SCO Xenix 386 ver 2.2.?.
:  If i have an open file and want to set that files length
:  to zero, do i have to close the file and reopen it.
:  I know if i open an existing file with creat it will truncate
:  the length to zero.  I want to be able to do that with a file
:  i have already opened and read from.

I am speaking from what I know of UNIX, so I don't know for sure
that Xenix does the same, but if you open the file again, with
the truncate bit set, it should set the file length to zero.  Of
course, you also have to seek the other file descriptor to the
beginning of the file.

In other words, if you know the file name, you could write
a routine like:

void
trunc_file(file, fd)
char    *file;
int     fd;
{
	lseek(fd, 0L, 0);
	close(open(file, O_TRUNC|O_WRONLY));
}

When it exits, your fd should point to the beginning of an empty
file.  (Of course, if you *want* to write to the old position,
you don't have to seek.)

---
Bill
novavax!proxftl!bill



More information about the Comp.lang.c mailing list