[代码分享] wxWidgets - wxDir 遍历文件

wxWidgets - wxDir 遍历文件

 



一、遍历单个目录下的文件 - 不进入子目录

 

 1     #include <stdio.h>
 2     #include "wx/filefn.h"
 3     #include "wx/dir.h"
 4 
 5     int main()
 6     {
 7         wxDir dir( wxGetCwd() );        //为枚举打开当前目录
 8 
 9         if ( !dir.IsOpened() )            //检测是否成功打开
10             return 0;
11 
12         puts("枚举当前目录下的文件:");
13 
14         wxString filename;
15 
16         bool cont = dir.GetFirst( &filename );
17         while ( cont )
18         {
19             printf("%s\n", filename.c_str());
20             cont = dir.GetNext(&filename);
21         }
22 
23         return 0;
24     }

 

 


    
二、遍历指定目录下所有文件 - 进入子目录

 

    #include <stdio.h>
    #include "wx/filefn.h"
    #include "wx/dir.h"

    int main()
    {
        wxDir dir( _T("D:\\Project\\wxWidgets\\wxDocToZh") );

        if ( !dir.IsOpened() )
            return 0;

        wxArrayString files;

        dir.GetAllFiles( _T("D:\\Project\\wxWidgets\\wxDocToZh"), &files );
        //获取 D:\\Project\\wxWidgets\\wxDocToZh 下所有文件的完整路径到 files

        unsigned int i = 0;
        for( i; i < files.GetCount(); i++ )
            puts( files[i] );

        return 0;
    }

 

 

 

 

三、通过 wxDir::Traverse 实现遍历目录下所有文件 - 进入子目录

 1 #include <stdio.h>
 2 #include "wx/filefn.h"
 3 #include "wx/dir.h"
 4 
 5 class wxDirTraverserSimple : public wxDirTraverser        //继承wxDirTraverser类
 6 {
 7 public:
 8     wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
 9 
10     virtual wxDirTraverseResult OnFile(const wxString& filename)
11     {
12         m_files.Add(filename);
13         return wxDIR_CONTINUE;
14     }
15 
16     virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
17     {
18         return wxDIR_CONTINUE;
19     }
20 
21 private:
22     wxArrayString& m_files;
23 };
24 
25 
26 int main()
27 {
28     wxDir dir( _T("D:\\Project\\wxWidgets\\wxDocToZh") );        //为枚举打开当前目录
29 
30     if ( !dir.IsOpened() )            //检测是否成功打开
31         return 0;
32 
33     wxArrayString files;
34     wxDirTraverserSimple traverser(files);
35 
36     dir.Traverse(traverser);
37 
38     unsigned int i = 0;
39     for(i; i < files.GetCount(); i++)
40         puts( files[i] );
41 
42     return 0;
43 }

 

 


    
四、其他操作

    wxDir::wxDir
    wxDir::~wxDir
    wxDir::Exists              //检测目录是否存在
    wxDir::GetAllFiles
    wxDir::FindFirst
    wxDir::GetFirst
    wxDir::GetName
    wxDir::GetNext
    wxDir::GetTotalSize        //获取目录大小
    wxDir::HasFiles            //目录是否存在文件
    wxDir::HasSubDirs         //是否存在子目录
    wxDir::IsOpened
    wxDir::Open
    wxDir::Traverse


    参见手册 wxWidgets - wxDir:
        笔者译: [译] wxWidgets - wxDir
        官方原文: http://docs.wxwidgets.org/2.8/wx_wxdir.html
        
        

 


--------------------



wid, 2012.03.01

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个完整的示例程序,在 wxWidgets 中使用 C++ 编写,可以遍历文件中所有的点画一个多边形: ```c++ #include <wx/wx.h> #include <wx/dcclient.h> #include <vector> #include <fstream> #include <sstream> class MyFrame : public wxFrame { public: MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); private: void OnPaint(wxPaintEvent& event); void DrawPolygon(wxDC& dc, const std::vector<wxPoint>& points); std::vector<wxPoint> m_points; wxDECLARE_EVENT_TABLE(); }; wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_PAINT(MyFrame::OnPaint) wxEND_EVENT_TABLE() MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(nullptr, wxID_ANY, title, pos, size) { // 从文件中读取点 std::ifstream input("points.txt"); std::string line; while (std::getline(input, line)) { std::istringstream iss(line); int x, y; if (iss >> x >> y) m_points.push_back(wxPoint(x, y)); } } void MyFrame::OnPaint(wxPaintEvent& event) { wxPaintDC dc(this); DrawPolygon(dc, m_points); } void MyFrame::DrawPolygon(wxDC& dc, const std::vector<wxPoint>& points) { // 遍历所有的点,并连接它们 for (size_t i = 0; i < points.size(); ++i) { if (i == 0) dc.DrawLine(points.back(), points[i]); else dc.DrawLine(points[i - 1], points[i]); } // 连接首尾两个点 dc.DrawLine(points.back(), points.front()); } class MyApp : public wxApp { public: bool OnInit() override; }; bool MyApp::OnInit() { MyFrame* frame = new MyFrame("Polygon Example", wxPoint(50, 50), wxSize(400, 300)); frame->Show(); return true; } wxIMPLEMENT_APP(MyApp); ``` 这个程序创建了一个 `MyFrame` 类来显示窗口,并在窗口中画出了一个多边形。程序从名为 `points.txt` 的文件中读取多边形的各个点,遍历所有的点并连接它们。注意要在最后连接首尾两个点。文件格式为每一行为一个点,两个数字中间用空格隔开,如: ``` 100 100 200 150 250 250 150 200 ``` 其中 `100 100` 表示第一个点的坐标为 `(100, 100)`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值