使用CRichEditCtrl的stream功能读写文件
2007-08-22 22:27
465 查看
网上的有些毛病
DWORD CALLBACK WriteFileCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG FAR* pcb)
{
CFile *pFile = (CFile*)dwCookie;
try
{
pFile->Write(pbBuff, cb);
*pcb = cb;
}
catch(CFileException* pEX)
{
pEX;
*pcb = 0;
return 1;
};
return 0;
}
DWORD CALLBACK ReadFileCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG FAR* pcb)
{
CFile *pFile = (CFile*)dwCookie;
try
{
*pcb = pFile->Read(pbBuff, cb);
}
catch(CFileException* pEX)
{
pEX;
*pcb = 0;
return 1;
};
return 0;
}
void CMyRichEditVIew::OnOpen()
{
if(!sFileName.IsEmpty()) {
CFile file(sFileName, CFile::modeRead | CFile::typeBinary);
EDITSTREAM stream;
stream.dwCookie = (DWORD)&file;
stream.pfnCallback = ReadFileCallBack;
GetRichEditCtrl().StreamIn(SF_TEXT, stream);
file.Close();
}
}
void CMyRichEditVIew::OnSave()
{
if(!sFileName.IsEmpty()) {
CFile file(sFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
EDITSTREAM stream;
stream.dwCookie = (DWORD)&file;
stream.pfnCallback = WriteFileCallBack;
GetRichEditCtrl().StreamOut(SF_TEXT, stream);
file.Close();
}
}
void CMyRichEditVIew::OnFileOpen()
{
// TODO: Add your command handler code here
CString fileName; //打开的文件名
//文件过滤器,打开文件时,文件列表中只出现过滤后的文件
static TCHAR szFilter[] = _T("文本文件 (*.log)|*.log|Rich Text Format (*.rtf)|*.rtf|全部文件 (*.*)|*.*||");
CFileDialog cfDlg(TRUE,NULL,_T("*.log"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,this);
if(cfDlg.DoModal() == IDOK)
fileName = cfDlg.GetFileName();
else
return;
//检查文件类型
int pos = fileName.Find(_T("."),0);
int extlength = fileName.GetLength() - pos -1;
CString fileType = fileName.Mid(pos+1,fileName.GetLength() - pos -1);
//打开文件
if(fileType.CompareNoCase(_T("log")) == 0)
{
CSerialLogDiffDoc * pDoc = (CSerialLogDiffDoc*)GetDocument();
pDoc->SetTitle(fileName);
SetFileName(fileName);
OnOpen();
ClearModifyFlag();
Parse();
}
}
void CMyRichEditVIew::OnFileSave()
{
// TODO: Add your command handler code here
CString fileName; //打开的文件名
//文件过滤器,打开文件时,文件列表中只出现过滤后的文件
static TCHAR szFilter[] = _T("文本文件 (*.log)|*.log|Rich Text Format (*.rtf)|*.rtf|全部文件 (*.*)|*.*||");
CFileDialog cfDlg(FALSE,NULL,_T("*.log"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,this);
if(cfDlg.DoModal() == IDOK)
fileName = cfDlg.GetFileName();
else
return;
//检查文件类型
int pos = fileName.Find(_T("."),0);
int extlength = fileName.GetLength() - pos -1;
CString fileType = fileName.Mid(pos+1,fileName.GetLength() - pos -1);
//打开文件
if(fileType.CompareNoCase(_T("log")) == 0)
{
CSerialLogDiffDoc * pDoc = (CSerialLogDiffDoc*)GetDocument();
pDoc->SetTitle(fileName);
SetFileName(fileName);
OnSave();
ClearModifyFlag();
}
}
DWORD CALLBACK WriteFileCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG FAR* pcb)
{
CFile *pFile = (CFile*)dwCookie;
try
{
pFile->Write(pbBuff, cb);
*pcb = cb;
}
catch(CFileException* pEX)
{
pEX;
*pcb = 0;
return 1;
};
return 0;
}
DWORD CALLBACK ReadFileCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG FAR* pcb)
{
CFile *pFile = (CFile*)dwCookie;
try
{
*pcb = pFile->Read(pbBuff, cb);
}
catch(CFileException* pEX)
{
pEX;
*pcb = 0;
return 1;
};
return 0;
}
void CMyRichEditVIew::OnOpen()
{
if(!sFileName.IsEmpty()) {
CFile file(sFileName, CFile::modeRead | CFile::typeBinary);
EDITSTREAM stream;
stream.dwCookie = (DWORD)&file;
stream.pfnCallback = ReadFileCallBack;
GetRichEditCtrl().StreamIn(SF_TEXT, stream);
file.Close();
}
}
void CMyRichEditVIew::OnSave()
{
if(!sFileName.IsEmpty()) {
CFile file(sFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
EDITSTREAM stream;
stream.dwCookie = (DWORD)&file;
stream.pfnCallback = WriteFileCallBack;
GetRichEditCtrl().StreamOut(SF_TEXT, stream);
file.Close();
}
}
void CMyRichEditVIew::OnFileOpen()
{
// TODO: Add your command handler code here
CString fileName; //打开的文件名
//文件过滤器,打开文件时,文件列表中只出现过滤后的文件
static TCHAR szFilter[] = _T("文本文件 (*.log)|*.log|Rich Text Format (*.rtf)|*.rtf|全部文件 (*.*)|*.*||");
CFileDialog cfDlg(TRUE,NULL,_T("*.log"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,this);
if(cfDlg.DoModal() == IDOK)
fileName = cfDlg.GetFileName();
else
return;
//检查文件类型
int pos = fileName.Find(_T("."),0);
int extlength = fileName.GetLength() - pos -1;
CString fileType = fileName.Mid(pos+1,fileName.GetLength() - pos -1);
//打开文件
if(fileType.CompareNoCase(_T("log")) == 0)
{
CSerialLogDiffDoc * pDoc = (CSerialLogDiffDoc*)GetDocument();
pDoc->SetTitle(fileName);
SetFileName(fileName);
OnOpen();
ClearModifyFlag();
Parse();
}
}
void CMyRichEditVIew::OnFileSave()
{
// TODO: Add your command handler code here
CString fileName; //打开的文件名
//文件过滤器,打开文件时,文件列表中只出现过滤后的文件
static TCHAR szFilter[] = _T("文本文件 (*.log)|*.log|Rich Text Format (*.rtf)|*.rtf|全部文件 (*.*)|*.*||");
CFileDialog cfDlg(FALSE,NULL,_T("*.log"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,this);
if(cfDlg.DoModal() == IDOK)
fileName = cfDlg.GetFileName();
else
return;
//检查文件类型
int pos = fileName.Find(_T("."),0);
int extlength = fileName.GetLength() - pos -1;
CString fileType = fileName.Mid(pos+1,fileName.GetLength() - pos -1);
//打开文件
if(fileType.CompareNoCase(_T("log")) == 0)
{
CSerialLogDiffDoc * pDoc = (CSerialLogDiffDoc*)GetDocument();
pDoc->SetTitle(fileName);
SetFileName(fileName);
OnSave();
ClearModifyFlag();
}
}
相关文章推荐
- 使用XML读写删除功能来实现资源文件配置
- QTextStream的使用 读写文件
- java 使用FileInputStream,FileReader,BufferedStream读写记事本文件的例子
- C++文件读写详解(ofstream,ifstream,fstream)还有简单使用
- 使用fstream,ifstream,ofstream读写文件的一种情形
- 使用XML读写删除功能来实现资源文件配置
- 使用gojs制作一个具备文件读写功能的家谱网页
- QT中使用QXmlStreamReader解析XML文件
- ofstream的使用方法--超级精细。C++文件写入、读出函数(转)
- C#使用FileStream文件读写
- java学习:使用dom4j读写xml文件
- 【转载】 C++文件读写详解(ofstream,ifstream,fstream)
- 使用java读写properties文件属性
- 使用cos.jar包实现文件上传功能
- 火花:使用文件管理功能管理我的连接
- Cmap和Carray以及Cstring的使用以及MFC中的文件读写
- 使用基本字节输入流,字节输出流一次读写一个字节来复制文本 文件
- C++文件读写详解(ofstream,ifstream,fstream)
- 当用fopen读写打开文件时,请使用r+,而不是rw
- [Linux流操作]使用fread和fwrite函数读写文件