相关部分代码如下:// If the user hit <ENTER>
if (nChar == 13)
{               
    // Figure out what line we're on
    CEdit& ed = GetEditCtrl();
    int iStart=0, iEnd=0;
    ed.GetSel(iStart, iEnd);
    int iLine = ed.LineFromChar(iStart);
    if (iLine > -1)
    {
        static char szLine[256];
        memset(szLine, 0, sizeof(szLine));
        // Get the whole line
        int iNdx = ed.GetLine(iLine, szLine, sizeof(szLine)-1);
        if (iNdx > 0)
        {                    
            // and Send() it out our socket
            strcat(szLine, "\r\n");            
            m_pSocket->Send(szLine, strlen(szLine));
        }
    }
}我看了一下szLine的长度是7,后面两个字符的值是16、76,请问是什么原因呢?

解决方案 »

  1.   

    我试着发了4个字符“aaaa”,结果szLine的长度还是7,最后三个是31、16、76;发三个字符的时候,好像szLine是正确的。
    另外我看了一下iLine的值是3
      

  2.   

    你先将szLinememset( szLine, 0, sizeof( szLine ) );
      

  3.   

    换成getwindowtext试试,你是只发一行,还是发整个edit的内容?
      

  4.   

    if( WM_KEYDOWN == pMsg->message )
    {
    if ( VK_RETURN == pMsg->wParam ) 
    {              
    // Figure out what line we're on 
    //CEdit& ed = GetEditCtrl(); 
    int iStart=0, iEnd=0; 
    m_edit1.GetSel(iStart, iEnd); 
    int iLine = m_edit1.LineFromChar(iStart); 
    if (iLine > -1) 

    char szLine[256]; 
    memset(szLine, 0, sizeof(szLine)); 
    // Get the whole line 
    int iNdx = m_edit1.GetLine(iLine, szLine, sizeof(szLine)-1); 
    if (iNdx > 0) 
    {                    
    // and Send() it out our socket 
    strcat(szLine, "\r\n");            
    //m_pSocket->Send(szLine, strlen(szLine)); 


    }
    }我做了个模拟程序,取出某行没问题。
      

  5.   

    你把static关键字去掉试试,我不知道有什么影响,我没怎么用过,程序先不用发了。
      

  6.   


    晕,真的是跟static有关呢,去掉就可以啦,谢谢啦