C++ strcpy_s()

来源:百度知道 编辑:UC知道 时间:2024/06/22 01:35:08
下面这个程序是在VC2005下写 编译没有通过 有谁能帮忙我改一下 并指出原因
#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
CString str = "beijing";
char a[1024];

strcpy_s(a,1024,str);
cout<<a<<endl;
cin.get();
return 0;
}
编译结果如下
1>strcpy.cpp
1>.\strcpy.cpp(12) : error C2065: “CString”: 未声明的标识符
1>.\strcpy.cpp(12) : error C2146: 语法错误 : 缺少“;”(在标识符“str”的前面)
1>.\strcpy.cpp(12) : error C2065: “str”: 未声明的标识符

参考代码:
#include "stdafx.h"
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
CString str = "beijing";
char a[1024];

strcpy_s(a,1024,str);
cout<<a<<endl;
cin.get();
return 0;
}

int main(){
string str="beijing";
char a[20];
memset(a,0,20);
memcpy(a,&str[0],7);
cout<<a<<endl;
cin.get();
return 0;
}

缺少头文件cstring
#include<afx.h>