# temp_file

Definition: | string filename = temp_file(string location="", prefix="", extn="tmp", open_mode="") --or-- {integer fn, string filename} = temp_file( ditto ) -- (if open_mode!="")
Description: | Create a temporary filename, which does not already exist, and optionally open it. location: The directory where the file should be created. If omitted it uses %TEMP% or %TMP%, or C:\temp\ or /tmp/. prefix: the (optional) first part of the filename. extn: the file extension to use, default is "tmp". open_mode: if not "", must be one of "w", "wb", "a", or "ab", in which case it returns an open file handle and string. If open_mode is not provided, there is a 1-in-a-million chance someone else will beat you to the punch; if you do provide one, it will open/loop for you, and return {fn,filename}. In the latter case you are expected to close and delete the file.
pwa/p2js: | Not supported.
Example: |
    pp(temp_file())
    {integer fn, string name} = temp_file("myapp/tmp","data","log","wb")
    pp({fn,name})
    close(fn)
    {} = delete_file(name)
    -- output:
    -- `C:\Users\Pete\AppData\Local\Temp\419750.tmp`
    -- {3, `C:\Program Files (x86)\Phix\myapp\tmp\data408865.log`}
See Also: | open, close, delete_file
Implementation: | See builtins\pfile.e (an autoinclude) for details of the actual implementation.
Technicalia | The location can actually be a file rather than a directory (compatible with Euphoria), not that I can think of any good reason why. The Euphoria version of this routine has an (also optional) integer fourth parameter, reserve_temp, which creates a (closed) empty file, instead of the string open_mode of the Phix version, and the default extension is "_T_"rather than "tmp". Otherwise the routines are (reasonably) compatible.