c++ 使用boost读取指定路径下的指定类型文件

boost::filesystem::recursive_directory_iterator it(inputFilePath);
fs::is_regular_file(*it) 判断是否是普通文件
it->path().extension() 获取文件后缀
it->path().string()   将整个文件路径转换为string
it->path().filename().c_str() 将文件名转换为string

//BOOST
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

namespace fs = boost::filesystem;
    std::string calibDir = argv[1];
    std::cout << "read cloundpoint file: " << calibDir << std::endl;
    bool res = get_data_filename(calibDir, data_filename);
    for (int i = 0; i < data_filename.size(); i++) {
        std::cout << "data_filename[" << i << "] = " << data_filename.at(i) << std::endl;
    }
bool get_data_filename(std::string calibDir, std::vector<std::string> &data_filename)
{
    data_filename.clear();

    if (!fs::exists(calibDir) && !fs::is_directory(calibDir)) {
        std::cerr << "# ERROR: Cannot find input directory " << calibDir << "." << std::endl;
        return false;
    }

    fs::path inputFilePath(calibDir);
    fs::recursive_directory_iterator it(inputFilePath);
    fs::recursive_directory_iterator endit;

    while (it != endit) {
        if (fs::is_regular_file(*it) && it->path().extension() == ".xyz")
        {
            std::string file_name = it->path().string();
            data_filename.push_back(file_name);
        } else {
            std::cout << it->path().extension() << ", not \".png\"" << std::endl;
        }

        it++;
    }

    if (data_filename.size() > 0) {
        return true;
    } else {
        return false;
    }
}

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值