我怎样才能将这些文件与修改日期时间排序文件夹中的文件列表 [英] how can I get a list of files in a folder in which the files are sorted with modified date time
问题描述
我需要的文件列表中的文件夹中,文件将其修改日期时间进行排序。
I need to a list of files in a folder and the files are sorted with their modified date time.
我用C ++工作在Linux下,Boost库的支持。
I am working with C++ under Linux, the Boost library is supported.
任何人都可以请我提供的如何实现此code的一些示例?
Could anyone please provide me some sample of code of how to implement this?
推荐答案
大多数操作系统不以任何特定的顺序返回目录条目。如果你想对它们进行排序(你应该如果你要显示的结果,人类用户),你需要做的是在一个单独的通行证。你能做到这一点的方法之一是项插入到的std :: multimap中
,像这样:
most operating systems do not return directory entries in any particular order. If you want to sort them (you probably should if you are going to show the results to a human user), you need to do that in a separate pass. One way you could do that is to insert the entries into a std::multimap
, something like so:
namespace fs = boost::filesystem;
fs::path someDir("/path/to/somewhere");
fs::directory_iterator end_iter;
typedef std::multimap<std::time_t, fs::path> result_set_t;
result_set_t result_set;
if ( fs::exists(someDir) && fs::is_directory(someDir))
{
for( fs::directory_iterator dir_iter(someDir) ; dir_iter != end_iter ; ++dir_iter)
{
if (fs::is_regular_file(dir_iter->status()) )
{
result_set.insert(result_set_t::value_type(fs::last_write_time(dir_iter->path()), *dir_iter));
}
}
}
您可以再通过 result_set
迭代,并映射的boost ::文件系统::路径
项将在升序
You can then iterate through result_set
, and the mapped boost::filesystem::path
entries will be in ascending order
这篇关于我怎样才能将这些文件与修改日期时间排序文件夹中的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!