我对字符的读写一直没怎么搞懂,
现在又遇到问题了
希望这次能在各位的指导下把这个问题搞明白任务: 将一个Edit(textEdit)中的文字按行读出来,并存入一个CString 的Array(m_textArray)
 m_nEditLineNum=m_textEdit.GetLineCount();
 for(int i=0;i<m_nEditLineNum;i++)
  {
      CString* m_pstrText=new CString();
int m_nLineLength;
m_nLineLength=m_textEdit.LineLength(i);
m_textEdit.GetLine(i,m_pstrText->GetBuffer(m_nLineLength));
         m_pstrText->ReleaseBuffer(-1);
m_textArray.Add(m_pstrText);
m_pstrText=NULL;
   }    注:该处申请的内存,在其他地方通过m_textArray释放这段程序在执行中,碰到这样的问题:
      1、当每行只有一个字符时,m_pstrText中没有存入任何字符
      
      2、如果有多个字符,第一行通常正常,但在第二行时,就会出现各种奇怪的问题,
          比如本来是abc,在watch窗口,本来应该是"abc",但现在却是"abc,后面少了一个后引号,
           或者,就是形如“abc吨”,(其中“吨”字没有那个“口”,我找不到那个字)。      3、  并且在m_pstrText->ReleaseBuffer(-1)这一步,经常报错,进入这个函数 ,
        发现它把参数nNewLength擅自改为其他一个比较大的值     这都是因为什么而引起的问题啊?

解决方案 »

  1.   

    我记得m_textArray.Add(m_pstrText)好象是一个copy的动作,而不是简单的改变指针.所以,我建议:m_textArray.Add(m_pstrText);
    m_pstrText=NULL;
    ===============>
    m_textArray.Add(m_pstrText);
    delete m_pstrText;
    m_pstrText=NULL;
      

  2.   

    to:bobob(静思--潜心研究PDF) 
      
         我试了一下,这样不行,我是这样理解的:
        
          m_textArray.Add(m_pstrText);只是将m_pstrText所指向的这快内存的地址加入自己
           的指针表中,
          并没有对该内存作任何操作,因此,此时作delete m_pstrText,就是将这块内存释放了        那么m_textArray中只保存了一个空指针,        我这样理解,对否?
      

  3.   

    for(int i=0;i<m_nEditLineNum;i++)
      {
          CString* m_pstrText=new CString();
    int m_nLineLength;
    m_nLineLength=m_textEdit.LineLength(i);
    m_textEdit.GetLine(i,m_pstrText->GetBuffer(m_nLineLength+1), m_nLineLength+1);
             m_pstrText->ReleaseBuffer(-1);
    m_textArray.Add(m_pstrText);
    m_pstrText=NULL;
       }
      

  4.   

    CStringArray sarrText;
    int nEditLineNum;
    CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT1); nEditLineNum=pEdit->GetLineCount();
    for(int i=0;i<nEditLineNum;i++)
    {
    CString pstrText;
    int nLineLength;
    nLineLength=pEdit->LineLength(i);
    pEdit->GetLine(i,pstrText.GetBuffer(nLineLength), nLineLength);
    pstrText.ReleaseBuffer(nLineLength);
    sarrText.Add(pstrText);
    }注:
      1. 这里不需要用指针,如果一定要用,也需要释放,否则会出现内存泄漏,因为 CStringArray.Add函数是拷贝内容而不是指针
      2. 最好要指定ReleaseBuffer函数的数据长度
      3. 至于你提到的第2个问题,可能是你在GetLine函数或ReleaseBuffer函数指定的数据长度超过了该行的实际数据长度,超过部分取出来的就是乱码了
      

  5.   

    我试了你的代码,m_pstrText是根本就取不到值的
      

  6.   

    GetBuffer
    不太会用,所以一般不用
      

  7.   

    我对CStringArray理解错了,的确不用new CString   按照 freemme() 的思路,重写了程序,第一个问题解决了,
      
       但乱码的问题依然存在
       
      

  8.   

    CEdit::GetLine
    int GetLine( int nIndex, LPTSTR lpszBuffer ) const;int GetLine( int nIndex, LPTSTR lpszBuffer, int nMaxLength ) const;Return ValueThe number of bytes actually copied. The return value is 0 if the line number specified by nIndex is greater then the number of lines in the edit control.ParametersnIndexSpecifies the line number to retrieve from a multiple-line edit control. Line numbers are zero-based; a value of 0 specifies the first line. This parameter is ignored by a single-line edit control.lpszBufferPoints to the buffer that receives a copy of the line. The first word of the buffer must specify the maximum number of bytes that can be copied to the buffer.nMaxLengthSpecifies the maximum number of bytes that can be copied to the buffer. GetLine places this value in the first word of lpszBuffer before making the call to Windows.ResCall this function to retrieve a line of text from an edit control and places it in lpszBuffer. The copied line does not contain a null-termination character.
      

  9.   

    第一,lpszBuffer的开头要指定一个缓冲区长度,所以我建议你用int GetLine( int nIndex, LPTSTR lpszBuffer, int nMaxLength )他会自动把nMaxLength做为缓冲区长度。
    第二,返回的字符串没有包括一个'\0'字符,这个我没注意到
      

  10.   

    首先,谢谢各位我发现了为什么会出现乱码了,但原因更令我迷惑现在的程序是:
      
        for(int i=0;i<m_nEditLineNum;i++)
        {
           CString m_strText;
          int m_nLineLength=0;
          m_nLineLength=m_textEdit.LineLength(i);
    m_textEdit.GetLine(i,m_strText.GetBuffer(m_nLineLength),m_nLineLength);
             m_strText.ReleaseBuffer(-1);
    m_textArray.Add(m_strText);
       }   每次循环中,不管该行有多少个字符,m_nLineLength得到的都是第一行的值,所以从第二行开始就会出现乱码。
       但为什么m_nLineLength会是第一行的值呢?而且如果没有对m_nLineLength赋初值为零的话,即申明如:int m_nLineLength; 一执行完这步,  就将第一次循环得到的字符个数赋给它。
       这真的很让我迷惑?
      

  11.   

    我找到问题了原来CEdit;;LineLength(i)这个函数中的i不是行数,而是字符索引值,MFC中的说明有错误。我把每行得到的长度叠加,在加上2(一对换行符),再赋给i,就正确了各位试一试,看是不是这样的。
      

  12.   

    同意楼主每次循环中,不管该行有多少个字符,m_nLineLength得到的都是第一行的值,但是我用了m_nLineLength=m_textEdit.LineLength(-1);就可以了,楼主试试