• wxWidgets流操作(二)wxTextInputStream和wxTextOutputStream


    这一节很短,但是站在这一节wxWidgest流操作(一)wxFileInputStream与wxFileOutputStream 肩上的。

    wxTextInputStream和wxTextOutputStream允许以行为单位来操纵流,特别适合于处理文本文件。

    利用这节的界面,打开源码后找到菜单File->&Open 子项的代码:

    void StreamAppFrame::OnStreamClick(wxCommandEvent& event)
    {
        wxString filename(wxT("streamMain.cpp"));
        wxString dummyfile(wxT("dummy.cpp"));
    
        wxFileInputStream inStream(filename);
        wxFileOutputStream outStream(dummyfile);
    
        
        int byteCount=inStream.GetLength();
        static char buffer[1024*1024*1024];
        wxString content;
        while(byteCount>0)
        {
            size_t bytesToRead=wxMin((size_t)sizeof(buffer),byteCount);
    
            if(inStream.Read((void*)buffer,bytesToRead).GetLastError()==wxSTREAM_NO_ERROR)
            {
                if(outStream.Write((void*)buffer,bytesToRead).GetLastError()==wxSTREAM_NO_ERROR)
                {
                    content=wxString::FromUTF8(buffer,bytesToRead);
                    byteCount-=bytesToRead;
                }
    
                *text1<<content;
                wxLogStatus(wxT("Copy file successfully."));
            }
            else
            {
                wxLogStatus(wxT("Failed to copy file."));
            }
        }
    
    
        *text1<<wxT("\n文件读取成功!\n");
    
      }
    

    从int byteCount开始至结束注释掉。下面将使用wxTextInputStream和wxTextOutputStream来重新实现该模块的功能以做比较。

    void StreamAppFrame::OnStreamClick(wxCommandEvent& event)
    {
        wxString filename(wxT("streamMain.cpp"));
        wxString dummyfile(wxT("dummy.cpp"));
    
        wxFileInputStream inStream(filename);
        wxFileOutputStream outStream(dummyfile);
    
        /*
        int byteCount=inStream.GetLength();
        static char buffer[1024*1024*1024];
        wxString content;
        while(byteCount>0)
        {
            size_t bytesToRead=wxMin((size_t)sizeof(buffer),byteCount);
    
            if(inStream.Read((void*)buffer,bytesToRead).GetLastError()==wxSTREAM_NO_ERROR)
            {
                if(outStream.Write((void*)buffer,bytesToRead).GetLastError()==wxSTREAM_NO_ERROR)
                {
                    content=wxString::FromUTF8(buffer,bytesToRead);
                    byteCount-=bytesToRead;
                }
    
                *text1<<content;
                wxLogStatus(wxT("Copy file successfully."));
            }
            else
            {
                wxLogStatus(wxT("Failed to copy file."));
            }
        }
    
    
        *text1<<wxT("\n文件读取成功!\n");
    
        */
    
      ...
    
    }
    

    wxTextInputStream和wxTextOutputStream的头文件为<wx/txtstrm.h>,将其加到源文件里来。

    wxTextInputStream和wxTextOutputStream的构造函数分别需要传入wxFileInputStream/wxFFileInputStream ,wxFileOutputStream/wxFFileOutputStream的实例,然后输入流时可以用wxTextInputStream.ReadLine()来从流中读取一行,用wxTextOutputStream.WriteString(wxString&)来将字符串输出到终端,两种流都重载了流操作符<<和>>。

        wxFFileInputStream fin(filename);
        wxFFileOutputStream fout(wxT("dummy1.cpp"));
        wxTextInputStream cin(fin);
        wxTextOutputStream cout(fout);
    
        while(1)
        {
            wxString got=cin.ReadLine();
            if(fin.Eof() && got.empty())
                break;
    
    
            *text1<<got<<wxT("\n");
            cout<<got<<wxT("\n");
    
    
    
        }
        text1->AppendText(wxT("搞定。"));
        wxLogStatus(wxT("读取并写入流完成。"));
    

    添加上面的代码到OnStreamClick方法的最后,重新编译,运行,一切OK!但实现却简单了很多,是不是。

    以下为OnStreamClick的完整代码:

    

    void StreamAppFrame::OnStreamClick(wxCommandEvent& event)
    {
        wxString filename(wxT("streamMain.cpp"));
        wxString dummyfile(wxT("dummy.cpp"));
    
        wxFileInputStream inStream(filename);
        wxFileOutputStream outStream(dummyfile);
    
        /*
        int byteCount=inStream.GetLength();
        static char buffer[1024*1024*1024];
        wxString content;
        while(byteCount>0)
        {
            size_t bytesToRead=wxMin((size_t)sizeof(buffer),byteCount);
    
            if(inStream.Read((void*)buffer,bytesToRead).GetLastError()==wxSTREAM_NO_ERROR)
            {
                if(outStream.Write((void*)buffer,bytesToRead).GetLastError()==wxSTREAM_NO_ERROR)
                {
                    content=wxString::FromUTF8(buffer,bytesToRead);
                    byteCount-=bytesToRead;
                }
    
                *text1<<content;
                wxLogStatus(wxT("Copy file successfully."));
            }
            else
            {
                wxLogStatus(wxT("Failed to copy file."));
            }
        }
    
    
        *text1<<wxT("\n文件读取成功!\n");
    
        */
        wxFFileInputStream fin(filename);
        wxFFileOutputStream fout(wxT("dummy1.cpp"));
        wxTextInputStream cin(fin);
        wxTextOutputStream cout(fout);
    
        while(1)
        {
            wxString got=cin.ReadLine();
            if(fin.Eof() && got.empty())
                break;
    
    
            *text1<<got<<wxT("\n");
            cout<<got<<wxT("\n");
    
    
    
        }
        text1->AppendText(wxT("搞定。"));
        wxLogStatus(wxT("读取并写入流完成。"));
    }
    
  • 相关阅读:
    重大技术需求系统八
    2020年下半年软考真题及答案解析
    周总结五
    重大技术需求系统七
    TextWatcher 编辑框监听器
    Android四大基本组件介绍与生命周期
    JAVA String,StringBuffer与StringBuilder的区别??
    iOS开发:保持程序在后台长时间运行
    宏定义的布局约束
    随便说一些
  • 原文地址:https://www.cnblogs.com/godspeedsam/p/2025709.html
Copyright © 2020-2023  润新知