mkstemp

Generate a unique temporary filename from templateString, creates and opens the file, and returns the open file and generated name.

The last six characters of template must be "XXXXXX" and these are replaced with a string that makes the filename unique.

The optional templateSuffix will be appended to the file name.

  1. Tuple!(File, "file", string, "name") mkstemp(in string templateString)
    version(Posix)
    Tuple!(File, "file", string, "name")
    mkstemp
    @trusted
    (
    in string templateString
    )
  2. Tuple!(File, "file", string, "name") mkstemp(in string templateString, in string templateSuffix)

Return Value

Type: Tuple!(File, "file", string, "name")

The open file and generated name.

Examples

1 import std.algorithm : startsWith;
2 import std.file : remove;
3 
4 auto tempFile = mkstemp(".unittest-XXXXXX");
5 scope (exit)
6     remove(tempFile.name);
7 
8 assert(tempFile.name.startsWith(".unittest-"));
9 assert(tempFile.file.isOpen);
10 assert(!tempFile.file.error);
11 tempFile.file.writeln("foobar");
12 tempFile.file.flush();
13 tempFile.file.rewind();
14 assert(tempFile.file.readln() == "foobar\n");

Meta