C++中,Boost库提供了boost::filesystem::directory_iterator类,用于遍历目录中的所有文件和子目录。它可以用于获取目录中的文件和子目录的名称、大小、修改时间等信息,也可以用于遍历目录中的所有文件。
下面是一个使用boost::filesystem::directory_iterator遍历目录中所有文件的例子:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::path path_to_dir = "/mnt/data/LCC/rosbag/cam_28/test02/pic";
for (const auto &entry : boost::filesystem::directory_iterator(path_to_dir))
{
if (boost::filesystem::is_regular_file(entry))
{ // 判断是否为普通文件
std::cout << entry.path() << " size: " << boost::filesystem::file_size(entry) << " bytes" << std::endl;
}
}
return 0;
}
输出结果:
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671719.871238.jpg" size: 167300 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671729.866418.jpg" size: 157458 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671710.672741.jpg" size: 176316 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671725.868740.jpg" size: 161791 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671705.474308.jpg" size: 220393 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671714.270738.jpg" size: 176927 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671726.668989.jpg" size: 168308 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671728.268088.jpg" size: 165919 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671704.476706.jpg" size: 225102 bytes
"/mnt/data/LCC/rosbag/cam_28/test02/pic/1682671716.471927.jpg" size: 186471 bytes