al@uniblab.UUCP (alan krantz) (09/13/88)
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. thanks allan krantz ....uunet!uniblab!al
bill@proxftl.UUCP (T. William Wells) (09/15/88)
In article <171@uniblab.UUCP> al@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
shirono@hcx3.SSD.HARRIS.COM (09/19/88)
/* Written 3:10 pm Sep 15, 1988 by chip@ateng.UUCP in hcx3:comp.lang.c */ According to english@stromboli.usc.edu (Joe English): >In article <171@uniblab.UUCP> al@uniblab.UUCP (alan krantz) writes: >> 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? > >Can't be done; you do have to close() and re- creat() or >open(... | O_TRUNC). int fd1, fd2; fd1 = creat("filename", 0666); write(fd1, "howdy", 5); fd2 = creat("filename", 0666); /* truncates file */ close(fd2); /* but fd1 is still open and available */ /* End of text from hcx3:comp.lang.c */ Also, fd1 is at offset 5. The next write would happen at that point, leaving a hole (or ASCII nul's). ______________________________________________________________________________ || Internet: shirono@ssd.harris.com Roberto Shironoshita || Harris Corporation || ...!novavax---\ Computer Systems Division || UUCP: ...!uunet-------!hcx1!shirono || ...!mit-eddie-/ ------------------------------------------------------------------------------ DISCLAIMER: The opinions expressed here are my own; they in no way reflect the opinion or policies of Harris Corporation.