strcpy and strcat cause problems sometimes

后端 未结 3 685
借酒劲吻你
借酒劲吻你 2021-01-26 22:10

hello I have a code like the one below

char *str ;

        strcpy(str, \"\\t<\");
        strcat(str, time);
        strcat(str, \">[\");
        strcat(s         


        
3条回答
  •  执笔经年
    2021-01-26 22:22

    This is much simpler and error-free from buffer overflows:

    #define BUFFERSIZE 512
    char str[BUFFERSIZE];
    
    snprintf(str, BUFFERSIZE, "\t<%s>[%s](%s) $ ", time, user, baseName);
    

提交回复
热议问题