CString遍历问题 定义如下:CString str="100 200";如何将100,200对CPoint对象pt进行赋值,使得pt.x==100、pt.y==200急用,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你这个要把数据提出来,然后把字符串转换成数字型,用int atoi( const char *str ); 就能转换 如果一个CString只是初始化一个CPoint对象的话 用下面的代码可以 如果初始化多个对象 就得用循环来做了ASSERT(str.Find(" ") != -1);pt.x = _ttoi(str.Left(str.Find(" ")));pt.y = _ttoi(str.Right(str.GetLength() - str.Find(" "))); 那顺便问一下CString str="100 200 300 400 500 600"赋值给三个CPoint对象的情况吧,谢谢了~ 呵呵,如果就是这个格式的话,按空格分隔,然后Trim提取。 闲着没事 给你写一个CString str = "123 121 234 235 345 346"; CPoint ptArr[3]; int iPos = 0; while (str.GetLength() > 0) { int iTemp = str.Find(" " , str.Find(" ") + 1); CString strTemp = str.Left(iTemp); if (iTemp == -1) { strTemp = str; iTemp = str.GetLength(); } ptArr[iPos].x = _ttoi(str.Left(str.Find(" "))); ptArr[iPos].y = _ttoi(str.Right(str.GetLength() - str.Find(" "))); iPos++; str = str.Mid(iTemp + 1 , str.GetLength() - iTemp); } 循环查找分隔符,找到数字的字符串,然后用atoi得到数值 为何不用_stscanf?CString str="100 200 300 400 500 600";CPoint pt[3];_stscanf(str, _T("%ld%ld%ld%ld%ld%ld"), &pt[0].x, &pt[0].y, &pt[1].x, &pt[1].y, &pt[2].x, &pt[2].y); 楼上的%ld与%ld之间应该用空格隔开吧 不需要空格,又不是printf输出,学C的时候scanf输入多个int,有见过中间加空格的么?它会自动去掉。 这位兄弟的是right......赞同仁兄! CString str( "100 200 120 563" );CString resToken;int curPos= 0;BOOL bXY=FALSE;resToken= str.Tokenize(" ",curPos);while (resToken != ""){ if(!bXY) pt.x = atoi(resToken.GetBuffer()); else pt.y = atoi(resToken.GetBuffer()); resToken.ReleaseBuffer( ); bXY=!bXY; resToken= str.Tokenize(" ",curPos);}; 由于CString所确定的分割符是是空格,所以我想在%ld与%ld也添加。后来测试了下 不添加可以是可以的可是如果分隔符是,或者_,那么就需要添加了。 正确的是:CString str="100 200 300 400 500 600";str.Replace(_T(" "), _T("\t")); // 先把空格换成TABCPoint pt[3];_stscanf(str, _T("%d\t%d\t%d\t%d\t%d\t%d"), &pt[0].x, &pt[0].y, &pt[1].x, &pt[1].y, &pt[2].x, &pt[2].y); #define MAX_LENGTH 10 CString strText(_T("100 200 300 400 500 600")); CString strResult(_T("")); CString strToken(_T(" ")); CPoint pt[MAX_LENGTH] = {0}; int nPos = 0; int nIndex = 0; strResult = strText.Tokenize(strToken, nPos); while(!strResult.IsEmpty()) { //strMsg += strResult + _T("\r\n"); if(nIndex % 2 ==0) pt[nIndex].x = _ttoi(strResult); else pt[nIndex].y = _ttoi(strResult); strResult = strText.Tokenize(strToken, nPos); nIndex++; } for(int i=0; i<nIndex; i++) { // 输出 0 - nIndex的所有CPoint pt[MAX_LENGHT] } 编写木马(毕业设计) 怎么让程序自动运行 用CListBox创建了一个窗口,怎么会双击没反映? 请问如何判断客户端与服务器是否还在连接中 ActiveX中创建的非模态对话框显示后,不能相应键盘操作?怎么解决? 给大家一个好网站,顺便放分 vc中一个进程如何获得另一个进程的按键消息 关于串口定义timer的问题 不明白,CSocket::Create和CSocket::Bind 里的地址和端口有什么区别? 关于CTreeCtrl控件的一个很棘手的问题 新线程没有立即运行 请问这两句代码是什么意思?
ASSERT(str.Find(" ") != -1);
pt.x = _ttoi(str.Left(str.Find(" ")));
pt.y = _ttoi(str.Right(str.GetLength() - str.Find(" ")));
CString str="100 200 300 400 500 600"
赋值给三个CPoint对象的情况吧,谢谢了~
CString str = "123 121 234 235 345 346";
CPoint ptArr[3];
int iPos = 0;
while (str.GetLength() > 0)
{
int iTemp = str.Find(" " , str.Find(" ") + 1);
CString strTemp = str.Left(iTemp);
if (iTemp == -1)
{
strTemp = str;
iTemp = str.GetLength();
} ptArr[iPos].x = _ttoi(str.Left(str.Find(" ")));
ptArr[iPos].y = _ttoi(str.Right(str.GetLength() - str.Find(" ")));
iPos++;
str = str.Mid(iTemp + 1 , str.GetLength() - iTemp);
}
CString str="100 200 300 400 500 600";
CPoint pt[3];
_stscanf(str, _T("%ld%ld%ld%ld%ld%ld"), &pt[0].x, &pt[0].y, &pt[1].x, &pt[1].y, &pt[2].x, &pt[2].y);
楼上的%ld与%ld之间应该用空格隔开吧
不需要空格,又不是printf输出,学C的时候scanf输入多个int,有见过中间加空格的么?它会自动去掉。
CString resToken;
int curPos= 0;
BOOL bXY=FALSE;resToken= str.Tokenize(" ",curPos);
while (resToken != "")
{
if(!bXY)
pt.x = atoi(resToken.GetBuffer());
else
pt.y = atoi(resToken.GetBuffer());
resToken.ReleaseBuffer( );
bXY=!bXY;
resToken= str.Tokenize(" ",curPos);
};
由于CString所确定的分割符是是空格,所以我想在%ld与%ld也添加。
后来测试了下 不添加可以是可以的
可是如果分隔符是,或者_,那么就需要添加了。
正确的是:CString str="100 200 300 400 500 600";
str.Replace(_T(" "), _T("\t")); // 先把空格换成TABCPoint pt[3];
_stscanf(str, _T("%d\t%d\t%d\t%d\t%d\t%d"), &pt[0].x, &pt[0].y, &pt[1].x, &pt[1].y, &pt[2].x, &pt[2].y);
CString strResult(_T(""));
CString strToken(_T(" "));
CPoint pt[MAX_LENGTH] = {0};
int nPos = 0;
int nIndex = 0; strResult = strText.Tokenize(strToken, nPos);
while(!strResult.IsEmpty())
{
//strMsg += strResult + _T("\r\n");
if(nIndex % 2 ==0)
pt[nIndex].x = _ttoi(strResult);
else
pt[nIndex].y = _ttoi(strResult);
strResult = strText.Tokenize(strToken, nPos);
nIndex++;
}
for(int i=0; i<nIndex; i++)
{
// 输出 0 - nIndex的所有CPoint pt[MAX_LENGHT]
}