strcpy、strncpy和strlcpy的用法

char * strcpy(char *to, const char *from)
{
       char *save = to;
 
       for (; (*to = *from) != '\0'; ++from, ++to);
       return(save);
}

char * strncpy ( char * destination, const char * source, size_t count )
{
    char *start = dest;

    while (count && (*dest++ = *source++))
             count--;
     if (count) //如果count大于零 即count is greater than the length of sources,'\0'已写入,不够的字符数都填充\             
     while (--count)
             *dest++ = '\0'; //继续写dest指向的字符
             return(start);
}    

size_t strlcpy(char *dest, const char *src, size_t size)
{
    size_t ret = strlen(src);

    if (size) {
        size_t len = (ret >= size) ? size - 1 : ret;   //防止源字符串的越界问题
        memcpy(dest, src, len);
        dest[len] = '\0';
    }
    return ret;
}

char * strncpy ( char * destination, const char * source, size_t num )
posted @ 2017-07-11 10:07  Tsunami_lj  阅读(500)  评论(0编辑  收藏  举报