strlcpy实现

#include <iostream>
using namespace std;
size_t strlcpy(char *dst, const char *src, size_t siz)
{
    char *d = dst;
    const char *s = src;
    size_t n = siz;
    if (n != 0 && --n != 0) {
        do {
            if ((*d++ = *s++) == 0)
                break;
        } while (--n != 0);
    }
    if (n == 0) {
        if (siz != 0)
            *d = '\0';         
        while (*s++)
            ;
    }
    return(s - src - 1);    
}
int main() {
    char a[20];
    int b = 0;
    b = strlcpy(a,"1234566a22",2);
    std::cout<<a<<std::endl;
    std::cout<<b<<std::endl;
    system("PAUSE");
    return 0;
}

 

posted on 2024-06-28 10:43  lydstory  阅读(3)  评论(0编辑  收藏  举报

导航