a portable way to truncate an open file
Tim Oldham
tjo at Fulcrum.BT.CO.UK
Sat May 13 02:14:18 AEST 1989
In article <268 at sawmill.UUCP> rjk at 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 at fulcrum.bt.co.uk
#include <stdisclaim>
Why have coffee, when caffeine tastes this good?
More information about the Comp.unix.questions
mailing list