Cstring string;
int j=0;
m_List.GetText(0,string.GetBuffer(100));
while(string.GetAt(j)!=' ')
    j++;
其中m_List是一个listbox的变量,其第一行的格式为
*******        *********
可运行后GetAt()函数出错,我用string.GetLength(),返回值是0。
可m_List.GetText(0,string.GetBuffer(100))的返回值却是对的。
请大侠们指点一下。
多谢了!!

解决方案 »

  1.   

    GetBuffer()之后,要ReleaseBuffer()吧。
    而且
    ClistBox::GetText有重载的
    int GetText( int nIndex, LPTSTR lpszBuffer ) const;

    void GetText( int nIndex, CString& rString ) const;
    你用第二种形式不好吗?
    m_List.GetText(0,&string);
      

  2.   

    对不起我没把代码贴全,后面有ReleaseBuffer()的
    而且第二种形式好象也有错。
      

  3.   

    应该是你调用ReleaseBuffer()的时机不对吧:
    Cstring string;
    int j=0;
    m_List.GetText(0,string.GetBuffer(100));
    // 这里应该立即调用!
    string.ReleaseBuffer();
    while(string.GetAt(j)!=' ')
        j++;
      

  4.   

    因为getat中索引越界,应先判断一下。
    CString str; m_list.GetText(0, str);
    for (int n = 0; n < str.GetLength(); n ++)
    char c = str.GetAt(n);
      

  5.   

    这个问题我知道原因,错就错在你用GetBuffer(100),这个函数分配缓冲区后GetLength()返回值是0,所以用GetAt()会有错,你需要用GetBufferSetLength(100)