2012-02-20 66 views
1

我想创建一个应用程序,使用C++的boost库在目录和子目录中搜索文件也我不想遇到像名为阿拉伯文件的文件的UNICODE文件的问题。 那我该怎么做呢?使用boost库在目录和子目录中搜索文件C++

UPDATE:

#include <iostream> 
#include <boost/filesystem/operations.hpp> 
#include <boost/filesystem/fstream.hpp> 
#define BOOST_FILESYSTEM_NO_DEPRECATED 
using namespace boost::filesystem; 
using namespace std; 

bool find_file(const path & dir_path,   // in this directory, 
       const std::string & file_name, // search for this name, 
       path & path_found)   // placing path here if found 
{ 
    if (!exists(dir_path)) return false; 
    directory_iterator end_itr; // default construction yields past-the-end 
    for (directory_iterator itr(dir_path); 
     itr != end_itr; 
     ++itr) 
    { 
    if (is_directory(itr->status())) 
    { 
     if (find_file(itr->path(), file_name, path_found)) return true; 
    } 
    else if (itr->path().filename() == file_name) // see below 
    { 
     path_found = itr->path(); 
     return true; 
    } 
    } 
    return false; 
} 

int main() 
{ 
    path myPath = "C:"; 
    string myFile = ".doc"; 
    path myfound = "c:"; 

    find_file(myPath, myFile, myfound); 
} 

我也试过这个代码,但它不会编译它表明这个错误并不少

undefined reference to `boost::filesystem3::path::filename() const 

也:

X:\mingw\boost\boost_1_47_0\boost\system\error_code.hpp|214|undefined reference to `boost::system::generic_category()'| 
+2

尝试boost :: filesystem – mikithskegg 2012-02-20 18:07:36

+0

我试过上面的代码,但徒劳无功我使用codeblocks和提升1_47_0 – pourjour 2012-02-20 18:35:06

+0

这不是一个编译器错误,这是一个链接器错误。 – ildjarn 2012-02-20 18:36:30

回答

9

你必须链接到boost_system和boost_filesystem库。如何做到这一点取决于你的编译器/链接器组合;例如,在我的系统上,我必须添加标志-lboost_system-mt -lboost_filesystem-mt

一些注释:在Windows上,您通常需要wstring(或其他“宽字符”对象)来增加处理Unicode路径的机会。其次,你可以让你的代码更短的使用find_ifrecursive_directory_iterator

#include <algorithm> 
#include <iostream> 

#define BOOST_FILESYSTEM_NO_DEPRECATED 
#define BOOST_FILESYSTEM_VERSION 3 

#include <boost/filesystem.hpp> 

using namespace std; 
using namespace boost::filesystem; 

bool find_file(const path& dir_path, const path& file_name, path& path_found) { 
    const recursive_directory_iterator end; 
    const auto it = find_if(recursive_directory_iterator(dir_path), end, 
          [&file_name](const directory_entry& e) { 
          return e.path().filename() == file_name; 
          }); 
    if (it == end) { 
    return false; 
    } else { 
    path_found = it->path(); 
    return true; 
    } 
} 

int main() { 
    const path myPath = L"/usr/local"; 
    const path myFile = L"filesystem.hpp"; 
    path myFound; 
    find_file(myPath, myFile, myFound); 
    wcout << myFound << endl; 
} 

我的示例使用C++ 11层的功能autolambda,这是目前在GCC 4.6。如果你的编译器没有这些,你可以很容易地更换由谓词对象的拉姆达和显式类型说明符的auto:使用Boost.Optional

#include <functional> 

class file_name_equal: public unary_function<path, bool> { 
public: 
    explicit file_name_equal(const path& fname): file_name(fname) { } 

    bool operator()(const directory_entry& entry) const { 
    return entry.path().filename() == file_name; 
    } 

private: 
    path file_name; 
}; 

bool find_file_cxx03(const path& dir_path, const path& file_name, 
        path& path_found) { 
    const recursive_directory_iterator end; 
    const recursive_directory_iterator it = 
    find_if(recursive_directory_iterator(dir_path), end, 
      file_name_equal(file_name)); 
    if (it == end) { 
    return false; 
    } else { 
    path_found = it->path(); 
    return true; 
    } 
} 

另一个不错的变体摆脱了返回值参考:

... 
#include <boost/optional.hpp> 

using namespace std; 
using namespace boost; 
using namespace boost::filesystem; 

optional<path> find_file(const path& dir_path, const path& file_name) { 
    const recursive_directory_iterator end; 
    const auto it = find_if(recursive_directory_iterator(dir_path), end, 
          [&file_name](const directory_entry& e) { 
          return e.path().filename() == file_name; 
          }); 
    return it == end ? optional<path>() : it->path(); 
} 

int main() { 
    const path myPath = L"/usr/local"; 
    const path myFile = L"filesystem.hpp"; 
    wcout << find_file(myPath, myFile).get_value_or("not found") << endl; 
} 
+0

哪一个可以帮助获得C:HDD的扩展名为.doc的所有文件 – pourjour 2012-02-21 00:09:02

+0

如何替换lambda功能\ – pourjour 2012-02-21 00:26:37

+0

我替换了lambda功能\ – pourjour 2012-02-21 00:26:43