您的位置:首页 > 编程语言

strncpy 在 vc runtime 里的实现以及测试代码

2012-11-20 15:05 281 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐