我的edit框是多行显示的(multiline),我知道可能得先读文件,将内容读入缓冲区,然后再在edit内显示。假如我的缓冲区只有10。即char buff[10],而我的.txt文件有1k。我的意思是每次读10个字节显示在edit框内,下一次10个字节怎么加入啊?都用哪些函数呢?谢谢

解决方案 »

  1.   

    第一次怎么做不用说了。第2次的10个字节这样读入:
    选中第十一个字节,然后用置换方法把第2次的10个字节置换进去。
    相关函数
    CEdit::SetSel 
    CEdit::ReplaceSel 
      

  2.   

    将文本载入一个串中,然后用SetWindowText()设置进去.
      

  3.   

    以下代码是从一个编辑框中读取,然后添加到另一个多行编辑框中UpdateData();
    CString strContext;


    CEdit *pEdit =(CEdit*)GetDlgItem(IDC_EDIT_ADD);
    pEdit->GetWindowText(strContext);
    pEdit = (CEdit*) GetDlgItem(IDC_EDIT_CONTEXT);
    // pEdit->GetWindowText(strContext);
    int nBegin,nEnd,nCount,nLength;
    nCount =pEdit->GetLineCount();
    /*
    if(nCount==1)
    {
    if(!strContext.IsEmpty())
    pEdit->SetWindowText(strContext+"\r\n");
    return;
    }
    */ if ((nBegin=pEdit->LineIndex(nCount-1)) != -1)
    {
    nLength=pEdit->LineLength(nCount-1);

    nEnd = nBegin +nLength ; 
    pEdit->SetSel(nEnd,-1);
    if(!strContext.IsEmpty())
    pEdit->ReplaceSel(strContext+"\r\n");
    }
      

  4.   

    while(feof(file_pointer))
    {
        fread(buf, sizeof(char), 10, file_pointer);
    }
      

  5.   

    char buffer_text[11];
    while(feof(file_pointer))
    {    fread(buf, sizeof(char), 10, file_pointer);
        strcpy(buffer_text, buf);
       strcat(buffer_text, "\n");
    }    }
      

  6.   

    从多行编辑框中取出保存txt文件,比较复杂的做法,简单的做法在差不多最后的地方注释了UpdateData();
    CString strName;
    CString strContext;
    CEdit *pEdit = (CEdit*) GetDlgItem(IDC_EDIT_NAME);
    pEdit->GetWindowText(strName);
    strName.TrimLeft();
    strName.TrimRight();
    strName += ".h";
    strName = "C:\\"+strName;
    CFile file;
    if( !file.Open(strName, CFile::modeCreate | CFile::modeWrite) )
       {
    #ifdef _DEBUG
    afxDump << "File could not be opened " << "\n";
    #endif
       }
    pEdit = (CEdit*) GetDlgItem(IDC_EDIT_CONTEXT); CString strTemp;
    pEdit->GetWindowText(strTemp);
    strTemp.TrimRight();
    pEdit->SetWindowText(strTemp); char *pbuf;
    int nCount = pEdit->GetLineCount();
    strTemp.Format("%d",nCount);
    AfxMessageBox(strTemp);
    for(int i=0;i<nCount;i++)
    {
    pbuf=new char[pEdit->LineLength(i)+1];
    pEdit->GetLine(i,pbuf);
    pbuf[pEdit->LineLength(i)]='\0';
    file.Write(pbuf,pEdit->LineLength(i));
    file.Write("\r\n",2);
    AfxMessageBox(pbuf);
    delete pbuf;
    pbuf =NULL;
    }

    // CString strTemp;

    // pEdit->GetWindowText(strTemp);
    // file.Write(strTemp,pEdit->GetWindowTextLength());
    pEdit = NULL;
    file.Close();

    CDialog::OnOK();
    以下代码是上面代码执行之后,从文件中读取添加到多行编辑框。
    ps:都是3个字母为一行UpdateData();
    CString strName;
    CString strContext="";
    CEdit *pEdit = (CEdit*) GetDlgItem(IDC_EDIT_NAME);
    pEdit->GetWindowText(strName);
    strName.TrimLeft();
    strName.TrimRight();
    strName += ".h";
    strName = "C:\\"+strName;
    MessageBox(strName);
    CFile file;
    if( !file.Open(strName, CFile::modeRead) )
       {
    #ifdef _DEBUG
    afxDump << "File could not be opened " << "\n";
    #endif
       } int nTemp =file.GetLength();
    strContext.Format("%d",nTemp);
    MessageBox(strContext);
    pEdit = (CEdit*) GetDlgItem(IDC_EDIT_CONTEXT); char *pbuf;
    UINT nBytesRead=1;
    int nCount=0;
    while(nCount <nTemp)
    {
    pbuf =new char[5];
    nBytesRead=file.Read(pbuf,5);
    nCount +=nBytesRead;
    pbuf[4]='\0'; strContext.Format("%4s",pbuf);
    strContext.TrimLeft();
    strContext.TrimRight(); int nBegin,nEnd,nCount,nLength;
    nCount =pEdit->GetLineCount(); if ((nBegin=pEdit->LineIndex(nCount-1)) != -1)
    {
    nLength=pEdit->LineLength(nCount-1);

    nEnd = nBegin +nLength ; 
    pEdit->SetSel(nEnd,-1);
    if(!strContext.IsEmpty())
    pEdit->ReplaceSel(strContext+"\r\n");
    }
    delete [] pbuf;
    } pEdit = NULL;
    file.Close();
      

  7.   

    文本框追加数据问题
    下面的代码在文本框最后追加"abc"int nLength = m_edit.SendMessage(WM_GETTEXTLENGTH);
    m_edit.SetSel(nLength, nLength);
    m_edit.ReplaceSel("abc");