Path: utzoo!attcan!uunet!husc6!rutgers!gatech!uflorida!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.unix.questions Subject: Re: Truncating an open file under BSD 4.3 Keywords: Empty, !ftruncate, fcntl? Message-ID: <12684@mimsy.UUCP> Date: 26 Jul 88 20:27:35 GMT References: <1392@valhalla.ee.rochester.edu> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 35 In article <1392@valhalla.ee.rochester.edu> badri@valhalla.ee.rochester.edu (Badri Lokanathan) writes: >What is the best way to empty an already open write-only file under BSD 4.3? >... I tried truncate but it leaves holes in the file, which make >it unreadable. You were on the right track. truncate (or better, ftruncate) does not `leave holes in the file'; rather, the sequence open seek write `leave(s) holes in the file'. Likewise, the sequence open write (moves seek pointer) ftruncate write will create holes, since the second `write' is not at the beginning of the file. Hence the proper sequence is (void) ftruncate(fd, 0L); (void) lseek(fd, 0L, 0); /* in either order */ or (if using stdio) (void) fflush(fp); (void) fseek(fp, 0L, 0); (void) ftruncate(fileno(fp), 0L); -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris