使用 boost 库 c++ 搜索目录和子目录中的文件

发布于 2025-01-07 05:14:47 字数 1512 浏览 0 评论 0原文

我想创建一个应用程序,使用 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:\mingw\boost\boost_1_47_0\boost\system\error_code.hpp|214|undefined reference to `boost::system::generic_category()'|

I want to create an application that search files in directory and in subdirectory using the boost library for c++ also I don't want to get trouble with UNICODE files like files named arabic .
So how can i do that?

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);
}

I tried also this code but it won't compile it show this error and a lot

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

also:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-01-14 05:14:47

您必须链接 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 中。如果您的编译器缺少这些,您可以轻松地用谓词对象替换 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.Optional 消除了返回值引用:

...
#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;
}

You have to link against the boost_system and the boost_filesystem libraries. How to do this depends on your compiler/linker combination; for example, on my system I have to add the flags -lboost_system-mt -lboost_filesystem-mt.

Some remarks: On Windows, you usually want wstring (or other "wide character" object) to increase your chance of working with Unicode paths. Second, you can make your code much shorter using find_if and 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;
}

My example uses the C++11 features auto and lambda, which are present in GCC 4.6. If your compiler lacks these, you can easily replace the lambda by a predicate object and the auto by an explicit type specifier:

#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;
  }
}

Another nice variant gets rid of the return value reference using Boost.Optional:

...
#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文