D string to C struct fixed-size array

bdh ben.h at posteo.net
Sun Jan 3 10:53:38 UTC 2021


On Sunday, 3 January 2021 at 09:28:55 UTC, rikki cattermole wrote:
> import std;
> void main()
> {
>     int[] a = [1, 2, 3, 4, 5];
>     int[3] b;
>
>     b[0 .. 3] = a[1 .. 4];
>     b.writeln;
> }
>
> Same principle, just remember to null terminate after slicing 
> your dynamic array and assigning it to your static array.

Thanks for the suggestion, however, it yields the same results as 
my "create new array and loop through string" attempt.  That is 
to say, an incomplete filename is used when writing (saving) the 
image to disk.

If it help, here is some of the actual code (using what I've 
understood from your sample):

     int main(string[] args)
     {
       ulong len;

       Image* image;
       /* a separate structure, which also has a 
char[MaxTextExtent] filename */
       ImageInfo* imageInfo;
       /* another GraphicsMagick struct */
       ExceptionInfo exception;

       InitializeMaigck(null);
       /* creates a ImageInfo with defaults */
       imageInfo = CloneImageInfo(null);

       /* actual code has a check for args.length */
       len = args[1].length;

       /* set the input filename */
       imageInfo.filename[0 .. len] = args[1];
       imageInfo.filename[len] = '\0';

       /* read image from input file */
       image = ReadImage(imageInfo, &exception);

       /* set the output filename */
       len = args[2].length;
       image.filename[0 .. len] = args[2];
       image.filename[len] = '\0';

       /* write (save) image to disk */
       WriteImage(imageInfo, image);

       /* memory cleanup skipped */

       return 0;
     }

The full code is really just a port of the `convert.c` example 
shown on the API page[0],

[0]: http://www.graphicsmagick.org/api/api.html




More information about the Digitalmars-d-learn mailing list