定义如下:
CString str="100 200";
如何将100,200对CPoint对象pt进行赋值,使得pt.x==100、pt.y==200
急用,谢谢!

解决方案 »

  1.   

    你这个要把数据提出来,然后把字符串转换成数字型,用int atoi(   const char *str ); 就能转换
      

  2.   

    如果一个CString只是初始化一个CPoint对象的话  用下面的代码可以  如果初始化多个对象 就得用循环来做了
    ASSERT(str.Find(" ") != -1);
    pt.x = _ttoi(str.Left(str.Find(" ")));
    pt.y = _ttoi(str.Right(str.GetLength() - str.Find(" ")));
      

  3.   

    那顺便问一下
    CString str="100 200 300 400 500 600"
    赋值给三个CPoint对象的情况吧,谢谢了~
      

  4.   

    呵呵,如果就是这个格式的话,按空格分隔,然后Trim提取。
      

  5.   

    闲着没事  给你写一个
    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);
    }
      

  6.   

    循环查找分隔符,找到数字的字符串,然后用atoi得到数值
      

  7.   

    为何不用_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);
      

  8.   


    楼上的%ld与%ld之间应该用空格隔开吧 
      

  9.   


    不需要空格,又不是printf输出,学C的时候scanf输入多个int,有见过中间加空格的么?它会自动去掉。
      

  10.   

    这位兄弟的是right......赞同仁兄!
      

  11.   

    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);
    };
      

  12.   


    由于CString所确定的分割符是是空格,所以我想在%ld与%ld也添加。
    后来测试了下 不添加可以是可以的
    可是如果分隔符是,或者_,那么就需要添加了。
      

  13.   


    正确的是: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);
      

  14.   

    #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]
    }