MFC阅读文本程序

来源:互联网 发布:晨枫网络是真的吗 编辑:程序博客网 时间:2024/05/19 04:53
/************************************************************************/
/* 阅读文本程序v1.1
/************************************************************************/
void CFunDlg::OnBtnRead() 
{
// TODO: Add your control notification handler code here
m_path ="d:\\note.txt";
CFile file;
if (!file.Open(m_path,CFile::modeRead))
{
MessageBox("Open File Erroe");
return ;
}


int len = file.GetLength();
char *buffer = new char[len+1];
if (!buffer)
{
MessageBox("Allocting Fail");
return;
}
else 
{
try
{
file.Read(buffer,len);//将文件中的内容读到缓冲区,C++会在结尾加入'\0'
}
catch (CFileException* e)
{
MessageBox("reading file error");
file.Close();
e->Delete();
return;
}

buffer[len]='\0';
m_note=buffer;

}
file.Close();
UpdateData(FALSE);
delete buffer; //释放动态申请的内存
}
原创粉丝点击