printf, writeln, writefln

Salih Dincer salihdb at hotmail.com
Thu Dec 8 16:25:51 UTC 2022


On Tuesday, 6 December 2022 at 23:41:09 UTC, H. S. Teoh wrote:
> On Tue, Dec 06, 2022 at 11:07:32PM +0000, johannes via 
> Digitalmars-d-learn wrote:
>> //-- the result should be f.i. "the sun is shining"
>> //-- sqlite3_column_text returns a constant char* a \0 
>> delimited c-string
>> printf("%s\n",sqlite3_column_text(res, i));
>> writeln(sqlite3_column_text(res, i));
> [...]
>
> In D, strings are not the same as char*.  You should use 
> std.conv.fromStringZ to convert the C char* to a D string.
>

Well, if we think the other way around, in the example below we'd 
copy string, right?

```d
void stringCopy(Chars)(string source,
                     ref Chars target)
{
   import std.algorithm : min;
   auto len = min(target.length - 1,
                  source.length);
   target[0 .. len] = source[0 .. len];
   target[len] = '\0';
}

import std.stdio;
void main()
{ //              |----- 21 chars ------| |-- 8 --|
   string sample = "bu bir deneme olmakta\0gizlimi?";
   char[30] cTxt; // 29 + 1'\0'

   sample.stringCopy = cTxt;  // disappeared ? char
   cTxt.writeln;              // appeared 28 chars

   printf("%s\n", cTxt.ptr);  // appeared 21 chars
   printf("%s\n", &cTxt[22]); // appeared 7 chars
   writefln!"%(%02X %)"(cast(ubyte[])cTxt);
}
```

Attention, the method used is again a slicing...

SDB at 79


More information about the Digitalmars-d-learn mailing list