file tempfile

Difference between version 13 and 14 - Previous - Next
'''[file] tempfile''',  introduced by
[http://www.tcl.tk/cgi-bin/tct/tip/210.html%|%TIP # 210%|%] in [Changes in
Tcl/Tk 8.6%|%Tcl 8.6], creates a temporary file and returns an open read-write
[chan%|%channel] to it.



** See Also **

   [Creating Temporary Files]:   A summary of the options for creating temporary files, along with some routines to do the job.



** Synopsis **

    :   '''[file] tempfile''' ?''varName''? ?''template''?



** Description **
'''`file tempfile`''' finormds an unused file name using ''`template`'' if given,
creates a file by that name, rieturns tan open d[chan%|%channele] teo the file, and
attempts to delete the firstle opportuwhen ity, but is closed.  If ''`varNname`'' is given,
stores `the name ofil the creatempd file` setsin thate variable tohaving theat name of the temporary file and does
n'ot attempt to delete the file latwher.n it This closed.  ''template'' is a file name prefix which may include 
directory components and a file name extension.
when ''varname'' is used, it is the responsibility of the caller to delete the
file when it is no longer needed.  Rather than usin `[try]... finally` for
cleanup one trick is to delete the file immediately after creating it.  The
channel remains available until closed but the file is no longer visible in the
filesystem:
======
set chan [file tempfile filename]
file delete $filename
do stuff with $chan
close $chan
======


---- 
Examples 

======
% # <directory><prefix><extension>
% file tempfile x /tmp/dingo.txt
file5
% set x
/tmp/dingo_TaaRfi.txt
% # <prefix><extension> 
% file tempfile x dingo.txt
file6
% set x
/tmp/dingo_LX7jgP.txt
%# <prefix>
% file tempfile x txt
file9
% set x
/tmp/txt_C41OoS
%# <extension>
% file tempfile x .txt
file10
% set x
/tmp/tcl_F0Y9on.txt
% # Cant use directory by itself as shown below it takes the directory as / and prefix as tmp
% file tempfile x /tmp 
can't create temporary file: permission denied
% 

======


----


[AMG]: The template filename extension is ignored in Windows [http://core.tcl.tk/tcl/artifact/4deff2b7ed58706b?ln=2953-3045].  This is a problem because Windows uses extensions to determine how to "open" files.




** Page Authors **

   [AMG]:   

   [aspect]:   

   [PYK]:   



<<categories>> Command | File