errno_t strcat_s(
char *strDestination,
size_t sizeInBytes,
const char *strSource
);
errno_t wcscat_s(
wchar_t *strDestination,
size_t sizeInWords,
const wchar_t *strSource
);
--------------------------------------------------
char buf[16];
strcpy_s(buf, 16, "Start");
strcat_s(buf, 16, " End"); // Correct
strcat_s(buf, 16 – strlen(buf), " End"); // Incorrect
--------------------------------------------------
//240TCHAR
TCHAR szDirPre[23] = {_T("一二三四五六七八九十")};
//17个TCHAR
TCHAR szFileName[10] = _T("1�ʹ�����9");
//正常
if(0)
{
int size = sizeof(szDirPre)/sizeof(TCHAR);
_tcscat_s(szDirPre, size, szFileName);
}
//正常 - 错误代码
if(0)
{
int size = sizeof(szDirPre)/sizeof(TCHAR);
int length1 = _tcslen(szDirPre);
int remain = size - length1;
int length2 = _tcslen(szFileName);
_tcscat_s(szDirPre, remain*sizeof(TCHAR), szFileName);
}
//异常
if(0)
{
int size = sizeof(szDirPre)/sizeof(TCHAR);
int length1 = _tcslen(szDirPre);
int remain = size - length1;
int length2 = _tcslen(szFileName);
_tcscat_s(szDirPre, remain, szFileName);
}
//异常
if(0)
{
_tcscat_s(szDirPre, sizeof(szDirPre), szFileName);
}
//正常
if(0)
{
int endIndex = _tcslen(szDirPre);
memcpy(szDirPre+endIndex, szFileName, _tcslen(szFileName)*sizeof(TCHAR));
endIndex += _tcslen(szFileName);
szDirPre[endIndex] = _T('\0');
}