使用CRichEditCtrl的stream功能读写文件

2019-07-14 09:12发布

 网上的有些毛病
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();
    }
}