// stringTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> #include <iostream> #include <strsafe.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { char szBuffer[10] = { 0 }; strcpy(szBuffer, "123456"); //1.error:strcpy(szBuffer, "1234567890"); //strncpy(szBuffer, "1234567890", 10 - 1);//2 strcat(szBuffer, "123"); //3.error:strcat(szBuffer, "1234"); //strncat(szBuffer, "1234", 10 - 1 -strlen(szBuffer)); //使用安全字符串函数 char szBuffer0[10] = { 0 }; //strcpy_s(szBuffer0, // 10,//目标缓冲区大小 // "1234567890" // );//这里就不会像上面1那样没有结束符(将会出问题),也不会像2那样截断(也许这更不是你想要的) strcpy_s(szBuffer0, 10, "123456");//重置 //strcat_s(szBuffer0, 10, "1234");//同strcpy_s //下面有截断的功能 char szBuffer1[10] = { 0 }; StringCchCopyA(szBuffer1, 10, "1234567890"); StringCchCopyA(szBuffer1, 10, "123456");//重置 StringCchCatA(szBuffer1, 10, "1234"); return 0; }