简体   繁体   English

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

[英]Is there any crossplatform C/C++ lib for files searching? (on hard drive)

Is there any crossplatform C\\C++ lib for files searching? 是否有用于文件搜索的跨平台C \\ C ++ lib? (on hard drive) What I need is simple - be able to find all images on users computer in all folders and subfolders with sise >= 200kb. (在硬盘上)我需要的是简单的 - 能够在所有文件夹和子文件夹中找到用户计算机上的所有图像,其中sise> = 200kb。

How to do such thing? 怎么办这样的事情? Can any one help me? 谁能帮我? Please. 请。

Boost.Filesystem is a great library. Boost.Filesystem是一个很棒的库。 Here is a code I wrote while ago, you can change the search criteria easily once you know the library(you can query the size and the extension): 这是我刚才写的代码,一旦你知道了库就可以轻松地更改搜索条件(你可以查询大小和扩展名):

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

ACE has a lot of cross-platform wrappers. ACE有很多跨平台的包装器。 In your particular case see ACE_Dirent or ACE_OS::scandir / ACE_OS::opendir / ACE_OS::readdir and friend functions. 在您的特定情况下,请参阅ACE_DirentACE_OS :: scandir / ACE_OS :: opendir / ACE_OS :: readdir和friend函数。 ACE is very powerful abstraction layer between operating systems. ACE是操作系统之间非常强大的抽象层。 If you need such things then this is the way to go. 如果你需要这样的东西,那么这就是你要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM