是否有用于文件搜索的跨平台C \ C++ lib? (在硬盘上)我需要的很简单 - 能够在所有文件夹和子文件夹中找到用户计算机上的所有图像,并且sise> = 200kb。是否有用于文件搜索的跨平台C/C++库? (在硬盘上)
如何做这样的事情?谁能帮我?请。
是否有用于文件搜索的跨平台C \ C++ lib? (在硬盘上)我需要的很简单 - 能够在所有文件夹和子文件夹中找到用户计算机上的所有图像,并且sise> = 200kb。是否有用于文件搜索的跨平台C/C++库? (在硬盘上)
如何做这样的事情?谁能帮我?请。
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;
}
ACE有很多跨平台的包装纸。在你的特定情况下,请参阅ACE_Dirent或ACE_OS::scandir/ACE_OS::opendir/ACE_OS::readdir和朋友功能。 ACE是操作系统之间非常强大的抽象层。如果你需要这样的事情,那么这就是要走的路。
他们现在有recursive_directory_iterator,所以这可以变得更简单! – Cubbi 2010-06-07 22:51:08
@Cubbi谢谢!我没有看看最近添加到文件系统,但肯定最好使用库设施而不是我的代码:) – AraK 2010-06-07 22:54:04