是否有适用于跨平台的C/C++文件搜索库?(在硬盘上)

3

有没有跨平台的C/C++文件搜索库?(在硬盘上)我需要的很简单 - 能够在用户计算机的所有文件夹和子文件夹中查找大小>= 200kb的所有图像。

如何实现这样的功能?有人可以帮助我吗?请。

2个回答

6

Boost.Filesystem 是一个非常好的库。以下是我之前写的一段代码,一旦你了解了这个库,就可以轻松地更改搜索条件(你可以查询文件大小和扩展名):

#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>

using namespace std;
using namespace boost::filesystem;

void find_file(const path& root, const string& file_name, vector<path>& found_files)
{
        directory_iterator current_file(root), end_file;
        bool found_file_in_dir = false;
        for( ; current_file != end_file; ++current_file)
        {
                if( is_directory(current_file->status()) )
                        find_file(*current_file, file_name, found_files);
                if( !found_file_in_dir && current_file->leaf() == file_name )
                {
                        // Now we have found a file with the specified name,
                        // which means that there are no more files with the same
                        // name in the __same__ directory. What we have to do next,
                        // is to look for sub directories only, without checking other files.
                        found_files.push_back(*current_file);
                        found_file_in_dir = true;
                }
        }
}

int main()
{
        string file_name;
        string root_path;
        vector<path> found_files;

        std::cout << root_path;
        cout << "Please enter the name of the file to be found(with extension): ";
        cin >> file_name;
        cout << "Please enter the starting path of the search: ";
        cin >> root_path;
        cout << endl;

        find_file(root_path, file_name, found_files);
        for( std::size_t i = 0; i < found_files.size(); ++i)
                cout << found_files[i] << endl;
}

2
现在他们有了recursive_directory_iterator,所以这可以变得更简单! - Cubbi
@Cubbi 谢谢!我还没有看过文件系统的最新添加,但肯定最好使用库设施而不是我的代码 :) - Khaled Alshaya

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接