Hello,
I am trying to write a program that saves multiple newly created data files that are named with a prefix that is included as one of the arguments. The aim is to create a string array of the output filename.
See the relevant piece of code below, with added printf statements I have used to try and debug:
size_t prefixLength = strlen(argv[3]);
printf("\nprefix length is %d\n",prefixLength);
char fout1[prefixLength+11];
char fout2[prefixLength+11];
char fout3[prefixLength+11];
char fout4[prefixLength+11];
char fout5[prefixLength+11];
char fout6[prefixLength+11];
strncpy(fout1,argv[3],prefixLength);
strncpy(fout2,argv[3],prefixLength);
strncpy(fout3,argv[3],prefixLength);
strncpy(fout4,argv[3],prefixLength);
strncpy(fout5,argv[3],prefixLength);
strncpy(fout6,argv[3],prefixLength);
printf("\n%s",fout1);
printf("\n%s",fout2);
printf("\n%s",fout3);
printf("\n%s",fout4);
printf("\n%s",fout5);
printf("\n%s\n",fout6);
strncat(fout1,".scale1.nii",(prefixLength+11));
strncat(fout2,".scale2.nii",(prefixLength+11));
strncat(fout3,".scale3.nii",(prefixLength+11));
strncat(fout4,".scale4.nii",(prefixLength+11));
strncat(fout5,".scale5.nii",(prefixLength+11));
strncat(fout6,".scale6.nii",(prefixLength+11));
printf("\n%s",fout1);
printf("\n%s",fout2);
printf("\n%s",fout3);
printf("\n%s",fout4);
printf("\n%s",fout5);
printf("\n%s\n",fout6);
I execute the program with "killme" as the 3rd argument and get the following output from the printf statements;
prefix length is 6
killme
killme
killme��ժ���p[=
killme����<���<<l��W�killme��ժ���p[=
killme
killme
killme.scale1.nii
e4.nii
killme��ժ���p[=.scale3.nii.scale4.nii
killme����<���<<l��W�killme��ժ���p[=.scale3.nii.scale4.nii
killme.scale5.nii
killme.scale6.nii
As you can see outputs 1,5, and 6 are correct, but, 2,3, and 4 are not. I do not understand why this is happening, but feel I must be making a basic error. Can anybody please explain this to me>
Thank you for any advice.
Joe