使用boost库c++搜索目录和子目录中的文件
Search files in directory and subdirectory using boost library c++
我想创建一个应用程序,使用c++的boost库搜索目录和子目录中的文件。我也不想在UNICODE文件(如名为arabic的文件)方面遇到麻烦。那么我该怎么做呢?
更新:
#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:mingwboostboost_1_47_0boostsystemerror_code.hpp|214|undefined reference to `boost::system::generic_category()'|
您必须链接到boostrongystem和boost_filesystem库。如何做到这一点取决于您的编译器/链接器组合;例如,在我的系统上,我必须添加标志-lboost_system-mt -lboost_filesystem-mt
。
备注:在Windows上,您通常希望wstring
(或其他"宽字符"对象)增加使用Unicode路径的机会。其次,您可以使用find_if
和recursive_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的特性auto
和lambda
,这两个特性出现在GCC 4.6中。如果编译器缺少这些,则可以很容易地将lambda替换为谓词对象,将auto
替换为显式类型说明符:
#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;
}
}
另一个不错的变体使用Boost去掉了返回值引用。可选:
...
#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;
}
相关文章:
- 使文件夹中的所有文件可供所有项目(子目录)访问
- 从所有列出的文件和子目录中更改目录
- 获取每个目录或子目录中第一个文件的路径
- Qt创建者:加载位于子目录中的项目文件
- 如何在子目录中创建文本文件?
- Makefile在Linux中工作,但在Windows下不起作用,在子目录中找不到文件
- 生成文件:子目录的模式规则
- CMake 子目录 - 编译测试子项目时,包含的头文件没有此类文件或目录
- 节点构建失败。无法在包含目录的子目录中找到库头文件
- 这是在生成文件中包含子目录的正确方法吗?
- 使用C 列出目录中的文件,而不是递归,只有文件和无子目录
- CMAKE:将静态库从子目录链接到另一个子目录中的可执行文件
- 在目录和子目录C 中计数具有特定扩展名的文件
- CMake 从子目录和父目录链接文件
- 创建目录中所有.cpp文件和 android.mk 子目录中的FILE_LIST
- 一种使用 boost 和 c++ 列出目录和子目录中所有文件的方法
- 如何在子目录中生成包含源的生成文件
- 用于列出子目录中所有文件的递归功能
- 扫描目录和子目录中的.txt文件
- 如何在cllion中编译子目录文件