如何将一个.txt文件的内容显示在edit框内? 我的edit框是多行显示的(multiline),我知道可能得先读文件,将内容读入缓冲区,然后再在edit内显示。假如我的缓冲区只有10。即char buff[10],而我的.txt文件有1k。我的意思是每次读10个字节显示在edit框内,下一次10个字节怎么加入啊?都用哪些函数呢?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一次怎么做不用说了。第2次的10个字节这样读入:选中第十一个字节,然后用置换方法把第2次的10个字节置换进去。相关函数CEdit::SetSel CEdit::ReplaceSel 将文本载入一个串中,然后用SetWindowText()设置进去. 以下代码是从一个编辑框中读取,然后添加到另一个多行编辑框中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"); } while(feof(file_pointer)){ fread(buf, sizeof(char), 10, file_pointer);} char buffer_text[11];while(feof(file_pointer)){ fread(buf, sizeof(char), 10, file_pointer); strcpy(buffer_text, buf); strcat(buffer_text, "\n");} } 从多行编辑框中取出保存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(); 文本框追加数据问题下面的代码在文本框最后追加"abc"int nLength = m_edit.SendMessage(WM_GETTEXTLENGTH);m_edit.SetSel(nLength, nLength);m_edit.ReplaceSel("abc"); 【求助】关于map和CStringList的问题 添加一个断开服务端按钮,如何添加响应函数,实现未开启服务的状态 想把配置文件放在当前程序路径appPath下一个appcfg文件夹里,请教怎么做 响应鼠标消息的难题 组合框的问题 谁有 G.723.1语音编解码算法 的相关资料啊?? 像一些图像处理软件,如PS,里面的标尺的功能是如何实现的? 为什么在VC中用float a = 4.1会出现warning? 如何访问ini文件 求DirectX3D中如何用三角形画球体 闹心的算法 哪位高手解释一下数据对齐的概念啊
选中第十一个字节,然后用置换方法把第2次的10个字节置换进去。
相关函数
CEdit::SetSel
CEdit::ReplaceSel
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");
}
{
fread(buf, sizeof(char), 10, file_pointer);
}
while(feof(file_pointer))
{ fread(buf, sizeof(char), 10, file_pointer);
strcpy(buffer_text, buf);
strcat(buffer_text, "\n");
} }
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();
下面的代码在文本框最后追加"abc"int nLength = m_edit.SendMessage(WM_GETTEXTLENGTH);
m_edit.SetSel(nLength, nLength);
m_edit.ReplaceSel("abc");