Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!ukc!axion!fulcrum!tjo From: tjo@Fulcrum.BT.CO.UK (Tim Oldham) Newsgroups: comp.unix.questions Subject: Re: a portable way to truncate an open file Keywords: truncate unix file Message-ID: <159@cat.Fulcrum.BT.CO.UK> Date: 12 May 89 16:14:18 GMT References: <268@sawmill.UUCP> Reply-To: tjo@fulcrum.bt.co.uk (Tim Oldham) Distribution: comp Organization: BT I.T. Systems, Birmingham, England Lines: 28 In article <268@sawmill.UUCP> rjk@sawmill.UUCP (Richard Kuhns) writes: > >I've got the strong feeling that this is something that should be >obvious and straight-forward, but for the life of me I can't think >of a *portable* way to do it. > >Under BSD, I can [f]truncate() it, but I can't think of a good way >to do it under System V, and the code has to run there too. I can't >do the truncate as part of the [f]open(), since I don't have the lock >yet. creat(filename, mode). If the file exists, it will be truncated. It's a while since I used BSD, but I seem to remember this is how I did it before I knew of truncate(2)'s existence, and it's fine on sysV. The mode and owner are left unchanged if the file exists, so no problems there. Of course, you'll have to close the file if you use the algorithm you posted, as you've opened it previously, and you hardly want to file descriptors. But I'd change the algorithm, anyway - what's the point opening the file before you're ready to write to it? Tim. -- Tim Oldham ...!mcvax!ukc!axion!fulcrum!tjo or tjo@fulcrum.bt.co.uk #include Why have coffee, when caffeine tastes this good?