Segmentation fault using strcat

前端 未结 5 1639
孤独总比滥情好
孤独总比滥情好 2020-12-04 03:16

Here is my code :

char *name, name_log=\"log-\";

------getting \'name\' from user-----

strcat(name_log, name);
char ext[] =         


        
5条回答
  •  一个人的身影
    2020-12-04 03:58

    string literals get allocated a fixed amount of memory, generally in a read only section, you instead need to use a buffer.

    char buffer[64] = "log-";
    strncat(buffer,".log",32);
    

    On a side note, strcat is generally unsafe, you need to use something that that checks the size of the buffer it uses or with limits on what it can concatenate, like strncat.

提交回复
热议问题