如何获取文件夹中文件的列表,该文件中的文件按修改的日期时间排序? [英] How to 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.
我正在Linux下使用C ++,支持Boost库.
I am working with C++ under Linux, the Boost library is supported.
任何人都可以向我提供一些有关如何实现此功能的代码示例吗?
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::filesystem::path
条目将以升序排列.
You can then iterate through result_set
, and the mapped boost::filesystem::path
entries will be in ascending order.
这篇关于如何获取文件夹中文件的列表,该文件中的文件按修改的日期时间排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!