Calling a C Function with C-style Strings

Adam D. Ruppe destructionator at gmail.com
Sat Nov 30 07:59:37 PST 2013


On Saturday, 30 November 2013 at 15:48:28 UTC, Nordlöw wrote:
>     /home/per/Work/justd/fs.d(1042): Error: function 
> core.sys.posix.stdlib.mktemp (char*) is not callable using 
> argument types (immutable(char)*)

This is because mktemp needs to write to the string. From 
mktemp(3):

  The last six characters of template must be  XXXXXX  and  these
  are  replaced  with  a string that makes the filename unique.  
Since it
  will be modified, template must not be a string constant, but 
should be
  declared as a character array.



So what you want to do here is use a char[] instead of a string. 
I'd go with:


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));
}


More information about the Digitalmars-d-learn mailing list