On 4/24/2011 5:22 PM, Justin Hanekom wrote:
Hi all,

I have what should be an *extremely* simple question that Im banging my head 
against: how to use
sprintf to format something to a string.

I have tried:
   import std.stdio;
   ...
   auto buffer = new char[12];
   auto chars_written = sprintf(cast(char *) buffer, "%d", 12345);
   writeln(chars_written);
and various other ways of getting the buffer into sprintf, but no matter what I 
do the program seems to
die when sprintf is called.

What I'm ultimately trying to do here is format a number with thousands 
separated by commas.

Any idea?

Thanks, Justin

sprintf is a C function. The string you passed needs to be zero-terminated. Since it's not, sprintf probably keeps trying to write more stuff until it segfaults, because it never finds the zero terminator. See std.string.toStringz (http://www.digitalmars.com/d/2.0/phobos/std_string.html#toStringz).

Reply via email to