strcpy_s和strcpy()函數的功能幾乎是一樣的。
strcpy函數,就象gets函數一樣,它沒有方法來保證有效的緩沖區尺寸,所以它只能假定緩沖足夠大來容納要拷貝的字符串。在程序運行時,這將導致不可預料的行為。用strcpy_s就可以避免這些不可預料的行為。
strcpy_s 一般使用三個參數
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
CString str0 = "這是一個測試"; int num = str0.GetLength(); char* result = new char[num + 1]; strcpy_s(result, num + 1, str0);
注意數組長度要加1,否則會進行報錯。
