strcpy_s

来源:互联网 发布:大数据概念股涨停 编辑:程序博客网 时间:2024/04/28 04:42

有如下代码:

char des[80] = {0};
std::string str = "hello";
strcpy_s(des, str.size(), str.c_str());

 

运行时出错

报错buffer太小

strcpy_s(des, str.size() + 1, str.c_str());就ok了 这个多出来的是用来放'\0'的

strcpy_s(des, _countof(des), str.c_str()); 这种方法也可以的

 

而且刚刚好

 

errno_t strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource );

第二个参数是元素的个数

而countof()正好是Compute the number of elements in a statically-allocated array.

 

刚刚合拍  如果定义数组的时候不是MAX_PATH,那在使用这些字符串函数时 用_countof是最好的

原创粉丝点击