Next: , Previous: , Up: Writing to Files   [Index]


7.3.5.4 make-temp-file

Some programs need to write temporary files. Here is the usual way to construct a name for such a file. The job of make-temp-file is to prevent two different users or two different jobs from trying to use the exact same file name.

Function: make-temp-file prefix &optional dir-flag suffix text

This function creates a temporary file and returns its name by adding to PREFIX some random characters that are different in each Emacs job.

PREFIX

a user-provided string that should be unique to this program; this helps Emacs generate a unique file name despite numerous programs in different jobs running simultaneously and using this macro.

DIR-FLAG

if non-nil, create a new, empty directory instead of a file.

SUFFIX

if non-nil, it is added to the end of the file name.

TEXT

if a string, insert it into the new file before returning; DIR-FLAG should be nil;

if nil, leave the file empty.

RETURN VALUE

the newly-created file name pointing to a new-created file that is empty unless TEXT contains a string, in which case the file contains TEXT.

If you want to write a temporary file which is likely to be small, you should compute the directory like this:

(make-temp-file
 (expand-file-name prefix
                   (or small-temporary-file-directory
                       temporary-file-directory)))

Listing 7.1: Creating a Small Temporary File