• 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("读取并写入流完成。"));
    }
    
  • 相关阅读:
    MyBatis执行sql的整个流程
    Ftp传输:向linux服务器上传文件时“550 Permission denied.”错误问题解决
    SpringBoot框架:两个方法同时调用时父方法使内部方法的DataSource注解失效的解决办法
    SpringBoot框架:通过AOP和自定义注解完成druid连接池的动态数据源切换(三)
    SpringBoot框架:配置文件application.properties和application.yml的区别
    SpringBoot框架:'url' attribute is not specified and no embedded datasource could be configured问题处理
    bash脚本打印字符串一个空格的内容
    gethostbyname的线程安全
    算法工程师的职业规划
    理解Deep Link & URI Schemes & Universal Link & App Link
  • 原文地址:https://www.cnblogs.com/godspeedsam/p/2025709.html
Copyright © 2020-2023  润新知