我有一个目录,其中包含一些具有不同类型扩展名的文件。其中,有.grb2文件(元数据)。例如:
...
20140729_0000_000.grb2
20140729_1200_000.grb2
20140730_1200_000.grb2
...
事实是,我想用C ++阅读仅此类型的最后修改文件(.grb2)。由于它们是按日期命名的,因此读取文件名中编号最大的文件也是有效的,因为这是最新的气象数据。我目前正在手动读取(直接在C ++中输入其文件名),这显然不是最佳选择,因为我下载了很多这样的文件。
你有什么主意吗 顺便说一句,我正在Windows中工作(如果有帮助的话)。
您可以考虑使用Boost Filesystem。Boost Filesystem为此实现了即将到来的标准库规范。
您可以使用它来编写更强大的代码并同时独立于平台:
for (auto&& entry : boost::make_iterator_range(fs::directory_iterator("."), {})) {
fs::path p = entry.path();
if (is_regular_file(p) && p.extension() == ".grb2")
{
std::time_t timestamp = fs::last_write_time(p);
if (timestamp > latest_tm) {
latest = p;
latest_tm = timestamp;
}
}
}
尽管此代码不是用记事本¹编写的,但已经过测试:
/tmp$
mkdir q
/tmp$
cd q
/tmp/q$
touch {a..z}.grb2
/tmp/q$
../test
报告:
Last modified: "./z.grb2"
/tmp/q$
touch k.grb2
/tmp/q$
../test
报告:
Last modified: "./k.grb2"
¹完全公开:它是在Ubuntu Linux上用Vim编写的
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句