2 u010983763 u010983763 于 2015.07.23 13:38 提问

strcpy和strcat的使用问题,搞不懂为什么出现这样的错误?怎么改呢》?

int _tmain(int argc, _TCHAR* argv[])

{

char n;
CString strTime; // 用于将CTime对象格式化为字符串

CTime curTime = CTime::GetCurrentTime();

cout<<GetExePath()<<endl;   

/*cout<<"请输入要删除几天前的文件夹:";
cin>>n;
*/
cout<<argv[1]<<endl;

n=_ttoi(argv[1]);

// cout<<n<<endl;
CTime t=curTime-CTimeSpan(n,0,0,0);

strTime = t.Format(_T("db%Y%m%d*"));

//wcout<<(LPCTSTR)strTime<<endl;

CString m=strTime;

strcpy_s(s,256,GetExePath());
cout<<s<<endl;

strcat_s(s,256,m);
cout<<s<<endl;
transfer(s);

return 0;

1>shijianbijiao1.cpp(197): error C2664: 'strcpy_s' : cannot convert parameter 3 from 'CString' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>shijianbijiao1.cpp(200): error C2664: 'strcat_s' : cannot convert parameter 3 from 'CString' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>

2个回答

oyljerry
oyljerry   Ds   Rxr 2015.07.23 13:49
已采纳

CString是Unicode编码,strcpy_s要求参数是char*,所以你需要先把CString类型转换为char类型
用CW2A

 strcat_s(s,256,CW2A(m));
VisualEleven
VisualEleven   Ds   Rxr 2015.07.25 09:26

用_tcscpy, _tcscat_s
或者将你的工程编码改成多字节编码

Csdn user default icon
上传中...
上传图片
插入图片