[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: temp files under C



On Tue, Jun 26, 2001 at 06:59:31PM +1000, Herbert Xu wrote:
> Brian May <bam@debian.org> wrote:
> 
> > what is the safest way to open/create a temporary file under C, and
> > get a string with the filename?
> 
> Call any function that returns a filename, then open it with O_EXCL, and
> finally fdopen it if you need a FILE *.

Remember that you need a fallback if the name turns out to be already
in use.  Even if the function you call checks for this, the name could
be snatched by another process just before you try to open it.  The
easiest solution is probably to have a loop around tmpnam(3) or one
of its friends.  Or reconsider why you need to know the name of a
temporary file :)

-- 
Richard Braakman
Will write free software for money.
See http://www.xs4all.nl/~dark/resume.html



Reply to: