c++小工具

本文汇总了C++中的一些常用小工具,包括文件遍历(可选boost库),文件目录的操作,字符分割和特殊字符处理,获取运行目录和环境变量的方法,以及时间计数的两种方式,同时涉及含中文字符串的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ 常用小工具

1. 文件遍历

  • 使用boost
#include <boost/filesystem.hpp>

using namespace boost::filesystem;

void readfiles(string dir){
  for(directory_iterator itr=directory_iterator(dir); itr!=directory_itr; ++itr){
    string filePath = itr->path().string();
    string fileName = itr->path().filename().string();
  }
}

CMakeLists.txt 中添加

target_link_libraries(main boost_system boost_filesystem)

  • 不使用boost
void TableDetector::getFileNames(std::string path, std::vector<std::string> &files){
    if(path[path.size()-1]=='/'){
        path = path.substr(0, path.size()-1);
    }

    DIR *dir;
    struct dirent *ptr;
    char base[1000];

    if ((dir=opendir(path.c_str())) == NULL) {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) {    ///current dir OR parrent dir
            continue;
        } else if(ptr->d_type == 8) {   ///file
            //printf("d_name:%s/%s\n",basePath,ptr->d_name);
            files.push_back(string(ptr->d_name));
        } else if(ptr->d_type == 10) {  ///link file
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值