strcpy_s() 怎么使用,请教大家!

nanyou1 2008-08-01 08:58:38
CString str = "beijing";
char a[1024];
strcpy_s(a,str);

出现下面的错误:

error C2665: 'strcpy_s' : none of the 2 overloads could convert all the argument types
 could be 'errno_t strcpy_s<1024>(char (&)[1024],const char *)'

我用的是visual studio 2005,我看别人的程序可以通过 strcpy_s(a,str); 把cstring类型str 复制到 char a[1024];
请问大家是不是要包含除<string.h>之外的头文件吗?

请问通过 strcpy_s(a,str); 是怎么样 才能把 cstring 类型的字符串 复制到 char a[1024]中,谢谢大家
...全文
3825 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunnyszc 2009-10-13
  • 打赏
  • 举报
回复
试一下这个MSDN
gql1123 2009-01-09
  • 打赏
  • 举报
回复
MSDN
elegant87 2009-01-09
  • 打赏
  • 举报
回复
MSDN这样解释的:
strcpy_s, takes the size of the buffer as a parameter, so it can determine if a buffer overrun will occur. If you use strcpy_s to copy eleven characters into a ten-character buffer, that is an error on your part; strcpy_s cannot correct your mistake, but it can detect your error and inform you by invoking the invalid parameter handler.



flydream1980 2009-01-08
  • 打赏
  • 举报
回复
放到 vc6.0里 发现没有 strcpy_s,msdn6.0也没有。
放到 vs2005里有了,msdn 2005也能查到了。
看来有些程序还是有点差别的。
sincor 2009-01-08
  • 打赏
  • 举报
回复
我自己重载 用自己的
dylrockies 2009-01-08
  • 打赏
  • 举报
回复
CString str = "aaaaaaaaaaaaaaa";//length = 1024
char buf[1025] = {0};
strcpy_s(buf,sizeof(buf) - 1,str.GetBuffer(str.GetLength(0));
K行天下 2008-08-01
  • 打赏
  • 举报
回复
这种东西查看msdn:
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);


template <size_t size>
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
例子:


// crt_strcpy_s.cpp
// This program uses strcpy_s and strcat_s
// to build a phrase.
//

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
char string[80];
// using template versions of strcpy_s and strcat_s:
strcpy_s( string, "Hello world from " );
strcat_s( string, "strcpy_s " );
strcat_s( string, "and " );
// of course we can supply the size explicitly if we want to:
strcat_s( string, _countof(string), "strcat_s!" );

printf( "String = %s\n", string );
}

Maxwell 2008-08-01
  • 打赏
  • 举报
回复
strcpy_s(a, (LPCSTR)str);
可能更符合实际情况。
Maxwell 2008-08-01
  • 打赏
  • 举报
回复
strcpy_s(a, sizeof(a), str);
strcpy_s(a, (LPCTSTR)str);

没测试。
herman~~ 2008-08-01
  • 打赏
  • 举报
回复
查看下msdn
icy_heart 2008-08-01
  • 打赏
  • 举报
回复
strcpy_s(a,str.c_str());
wangdeqie 2008-08-01
  • 打赏
  • 举报
回复
strcpy_s(a,1024,str);

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧