问题描述
本人使用Microsoft Visual Studio编写C++MFC代码。
在调用 sscanf() 函数的过程中:
CString str;
char m_port[255]="";
str.Format(_T("fpatemp:%.1f,devtmp:%.1f,fpaAvg:%d,ave:%d--max:%.1f℃(%d),min:%.1f℃(%d),center:%.1f℃"),fpatmp,coretemp,fpaavg,outavg,tempmax,pData[t+4],tempmin,pData[t+7],tempcenter);
sscanf(str,"%s",m_port); // 这一步报错
编译器报错:
error C2664: ‘sscanf’ : cannot convert parameter 1 from ‘CString’ to 'const char ’
无法将CString类型转换为 const char 类型
解决方案
调用函数 _stscanf( ) 及 _T( );
将错误步骤改写为:
_stscanf(str,_T("%s"),m_port);
改写后,程序编译成功。