tempFile

Creates a temporary file. The file is automatically deleted when it is no longer referenced. The temporary file is always opened with both read and write access.

  1. TempFile!(F, string) tempFile(AutoDelete autoDelete, string dir)
    version(Posix)
    TempFile!(F, string)
    tempFile
    (
    F = File
    )
  2. TempFile!(F, T) tempFile(AutoDelete autoDelete, T dir)

Parameters

autoDelete AutoDelete

If set to AutoDelete.yes (the default), the file is deleted from the file system after the file handle is closed. Otherwise, the file must be deleted manually.

dir string

Directory to create the temporary file in. By default, this is tempDir.

Examples

Creates a temporary file and writes to it.

auto f = tempFile.file;
assert(f.position == 0);
f.write("Hello");
assert(f.position == 5);

Creates a temporary file, but doesn't delete it. This is useful to ensure a uniquely named file exists so that it can be written to by another process.

auto path = tempFile(AutoDelete.no).path;

Meta