Search code examples
cconcatenationc-stringsstrcatnull-character

why these two character arrays won't concatenate properly?(C Programming)


char *program_working_dir;
char backup_dir[9]="/Backups/";

// getting the current working directory
program_working_dir = getcwd(NULL, 0);
if (program_working_dir == NULL){
    printf("Failed to get working directory( take_backup function )\n");
    return -1;
}

// allocate memory for full path that contain the current directory and the /Backups/
char *full_backup_dir = (char *)malloc( (strlen(program_working_dir) + strlen(backup_dir) + 1 ) * sizeof(uint8_t) );
if (full_backup_dir == NULL){
    printf("Failed to allocate proper memory to stor full path of backup directory\n");
    return -1;
}

// used for debugging purposes
printf("program working dir%s\n", program_working_dir); // (here : /home/ramixix/Documents/3_grade/2th_Semester/image_processing/Project/image_processing/)
printf("%d\n",  strlen(program_working_dir));  // ( 86 length of program_working_dir )

// try to concatenate the tow string using snprintf. I add 1 to size for '\0' terminate string
snprintf(full_backup_dir, ( strlen(program_working_dir) + strlen(backup_dir) +1  ) * sizeof(uint8_t)  , "%s%s", program_working_dir, backup_dir);
printf("Full path: %s\n", full_backup_dir);

Here I simply trying to concatenate two string (path) together, but when sometimes that I execute this it does not work properly. for example i get this:

program working dir/home/ramixix/Documents/3_grade/2th_Semester/image_processing/Project/image_processing
86
path /home/ramixix/Documents/3_grade/2th_Semester/image_processing/Project/image_processing/Backups/��M�X

As you can see here after concatenation I get the wired string /��M�X and this ruin all my program, I do not understand why this happens. I also try to do the same thing using strncpy and strncat,So I replace the line:

snprintf(full_backup_dir, ( strlen(program_working_dir) + strlen(backup_dir) +1  ) * sizeof(char)  , "%s%s", program_working_dir, backup_dir);

To

strcpy(full_backup_dir, program_working_dir);
strcat(full_backup_dir, backup_dir);

Still have the same problem. I also remove the 1 that i added to the size of full_back_dir but again this program don't want to run properly. At this point I can really appreciate any help and feedback. PLEASE HELP!!!!


Solution

  • This array

    char backup_dir[9]="/Backups/";
    

    does not contain a string because it has no enough space to store the terminating zero '\0' of the string literal used as an initializer.

    Declare the array like

    char backup_dir[] = "/Backups/";
    

    In fact there is no need to calculate the length of the stored string using the function strlen. It is equal to sizeof( backup_dir ) - 1.

    Also in this expression

     (strlen(program_working_dir) + strlen(backup_dir) + 1 ) * sizeof(uint8_t)
    

    the operand sizeof(uint8_t) is redundant and only confuses readers of the code.

    The above expression could be written like

    strlen( program_working_dir ) + sizeof( backup_dir )