#include "stdafx.h"
#include <iostream>
/***
*
*Purpose:
* defines strncpy() -
*
***********************************************************/
char *__cdecl My_strncpy(char *dest, const char* src,size_t count)
{
char *start = dest;
while (count && (*dest++ = *src++))
{
count--;
}
if (count)
{
while (--count)
{
*dest++ = '\0';
}
}
return start;
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* tmpBigSrc = "1234567890123";
const char* tmpSmallSrc = "12345";
char chDes[10];
My_strncpy(chDes,tmpBigSrc,sizeof(chDes)-1);
chDes[sizeof(chDes)-1]='\0';
std::cout<<"dest is "<<chDes<<std::endl;
My_strncpy(chDes,tmpSmallSrc,sizeof(chDes)-1);
chDes[sizeof(chDes)-1]='\0';
std::cout<<"dest is "<<chDes<<std::endl;
system("pause");
return 0;
}
本文详细解析了一个自定义的C++字符串复制函数My_strncpy的实现原理及用法,通过实例展示了如何使用该函数进行字符串复制,并讨论了与标准库函数strncpy的区别。
1279

被折叠的 条评论
为什么被折叠?



