如何将一个.txt文件的内容显示在edit框内?

sdcer777 2003-12-29 12:22:34
我的edit框是多行显示的(multiline),我知道可能得先读文件,将内容读入缓冲区,然后再在edit内显示。假如我的缓冲区只有10。即char buff[10],而我的.txt文件有1k。我的意思是每次读10个字节显示在edit框内,下一次10个字节怎么加入啊?都用哪些函数呢?谢谢
...全文
111 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluebohe 2003-12-29
  • 打赏
  • 举报
回复
文本框追加数据问题
下面的代码在文本框最后追加"abc"

int nLength = m_edit.SendMessage(WM_GETTEXTLENGTH);
m_edit.SetSel(nLength, nLength);
m_edit.ReplaceSel("abc");

kongyunzhongque 2003-12-29
  • 打赏
  • 举报
回复
从多行编辑框中取出保存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();
myemailcn 2003-12-29
  • 打赏
  • 举报
回复
char buffer_text[11];
while(feof(file_pointer))
{

fread(buf, sizeof(char), 10, file_pointer);
strcpy(buffer_text, buf);
strcat(buffer_text, "\n");
}



}
myemailcn 2003-12-29
  • 打赏
  • 举报
回复
while(feof(file_pointer))
{
fread(buf, sizeof(char), 10, file_pointer);
}
kongyunzhongque 2003-12-29
  • 打赏
  • 举报
回复
以下代码是从一个编辑框中读取,然后添加到另一个多行编辑框中

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");
}
softworms 2003-12-29
  • 打赏
  • 举报
回复
将文本载入一个串中,然后用SetWindowText()设置进去.
BinaryTreeEx 2003-12-29
  • 打赏
  • 举报
回复
第一次怎么做不用说了。第2次的10个字节这样读入:
选中第十一个字节,然后用置换方法把第2次的10个字节置换进去。
相关函数
CEdit::SetSel
CEdit::ReplaceSel

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧