Thread: Unpredicted outcome using char array, strncpy, and strncat

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Unpredicted outcome using char array, strncpy, and strncat

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well strncpy doesn't automatically append a \0.
    You have to make sure it's a proper string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It appears that you're not reserving enough room for the end of string characters. Also have you considered using sprintf() to piece together the strings?

  4. #4
    Registered User
    Join Date
    Mar 2018
    Posts
    2
    Quote Originally Posted by jimblumberg View Post
    It appears that you're not reserving enough room for the end of string characters. Also have you considered using sprintf() to piece together the strings?
    [QUOUTE=Salem;]Well strncpy doesn't automatically append a \0.
    You have to make sure it's a proper string.[/QUOTE]

    Thanks both. I didn't consider the use of sprintf, but perhaps that might be easier.

    Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 12-15-2016, 08:06 PM
  2. String operations: strncat, strncpy used right?
    By tinchi in forum C Programming
    Replies: 5
    Last Post: 10-15-2014, 09:26 AM
  3. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  4. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  5. Replies: 3
    Last Post: 11-17-2008, 12:36 PM

Tags for this Thread