Getting a safe path for a temporary file

Shriramana Sharma not.sure.why.a.mail.id.is.needed.even.if.invalid at gmail.com
Sun Oct 22 15:21:37 UTC 2017


On Saturday, 17 January 2015 at 17:16:41 UTC, Tobias Pankrath 
wrote:
> You're looking for core.sys.posix.stdlib : mkstemp.
>
> I think that should be used by std.stdio.File as well, care to 
> create an enhancement request in bugzilla?

Though this thread is old, I ran into the issue when wanting to 
create a temporary file in my D program and so filed this: 
https://issues.dlang.org/show_bug.cgi?id=17926

For my program right now I'm using a souped-up version using a 
static array:

import std.stdio;

struct TempFile
{
     string name;
     private File handle;
     alias handle this;
}

TempFile tempFileOpen()
{
     char[20] name = "/tmp/XXXXXX";
     File handle;

     import core.sys.posix.stdlib: mkstemp;
     handle.fdopen(mkstemp(name.ptr), "w");

     import std.string: fromStringz;
     return TempFile(fromStringz(name.ptr).idup, handle);
}

void main()
{
     TempFile tfile;
     tfile = tempFileOpen();
     writeln(tfile.name);
     tfile = tempFileOpen();
     writeln(tfile.name);
}



More information about the Digitalmars-d-learn mailing list