实现功能:
拷贝任意字符串的子字符串.
使用函数:strncpy_s
MSDN解释:
errno_errno_t strncpy_s(
char *strDest,
size_t numberOfElements,
const char *strSource,
size_t count
);
其中各个变量的含义:
strDest:目标字符串
numberOfElements:目标字符串的大小(以字符为单位)
strSource:源字符串
count:要拷贝的字符串的字符数
Demo:
将morning拷贝到一个新的变量中
char source[100]="good morning everyone and hope you have a good time this winter!!";
char dest[30];
strncpy_s(dest,30,source+5,7);
对于宽字符使用如下:
errno_t wcsncpy_s(
wchar_t *strDest,
size_t numberOfElements,
const wchar_t *strSource,
size_t count
);
方法二:
void *_memccpy(
void *dest,
const void *src,
int c,
size_t count
);
解释:
c为最后一个要拷贝的字符
count为dest中可容纳的最大字符数
功能就是将从source开始到c的字符串拷贝到新的变量中。
void test()
{
char buffer[61];
char *pdest;
char string1[60] = "The quicks brown dog jumps over the lazy fox";
printf( "Function: _memccpy 60 characters or to character 's'\n" );
printf( "Source: %s\n", string1 );
pdest = (char*)_memccpy( buffer, string1+4, 's', 60 );
*pdest = '\0';
printf( "Result: %s\n", buffer );
printf( "Length: %d characters\n", strlen( buffer ) );
}