この特定の問題の場合:
これは、mktemp が文字列に書き込む必要があるためです。mktemp(3) から:
テンプレートの最後の 6 文字は XXXXXX である必要があり、これらはファイル名を一意にする文字列に置き換えられます。テンプレートは変更されるため、文字列定数ではなく、文字配列として宣言する必要があります。
したがって、ここでやりたいことは、文字列の代わりに char[] を使用することです。私は一緒に行きます:
import std.stdio;
void main() {
import core.sys.posix.stdlib;
// we'll use a little mutable buffer defined right here
char[255] tempBuffer;
string name = "alphaXXXXXX"; // last six X's are required by mktemp
tempBuffer[0 .. name.length] = name[]; // copy the name into the mutable buffer
tempBuffer[name.length] = 0; // make sure it is zero terminated yourself
auto tmp = mktemp(tempBuffer.ptr);
import std.conv;
writeln(to!string(tmp));
}
一般に、変更可能な文字列を作成するには、次の 2 つの方法のいずれかを使用できます。
toStringz
入力データが可変かどうかは気にせず、常に不変を返します (明らかに...)。しかし、それを自分で行うのは簡単です:
auto c_str = ("foo".dup ~ "\0").ptr;
それがあなたのやり方です。
string name = "alphaXXXXXX"; // last six X's are required by mktemp
auto tmp = mktemp((name.dup ~ "\0").ptr);