• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MKT-porter
博客园    首页    新随笔    联系   管理    订阅  订阅
c++ 获取文件夹目录名字

 

 

 

 

main.cpp

#ifndef PHOTO_FILE_PROCESSOR_H
#define PHOTO_FILE_PROCESSOR_H

#include <iostream>
#include <string>
#include <vector>
#include <dirent.h>
#include <algorithm>
#include <stdexcept>

class PhotoFileProcessor {
public:
    // 构造函数,初始化目录路径
    explicit PhotoFileProcessor(const std::string& directory_path)
        : directoryPath(directory_path) {}

 // 获取文件名(不带扩展名)并按时间戳排序的实现
    std::vector<std::string> getFilenamesSortedByTimestamp() const {
        DIR* dir = opendir(directoryPath.c_str());
        if (dir == nullptr) {
            throw std::runtime_error("无法打开目录: " + directoryPath);
        }

        std::vector<std::pair<double, std::string>> fileTimestampPairs;
        struct dirent* entry;

        while ((entry = readdir(dir)) != nullptr) {
            std::string filename(entry->d_name);

            // 过滤掉"."和".."目录
            if (filename == "." || filename == "..") {
                continue;
            }

            // 去除文件扩展名
            std::string filename_without_extension = removeExtension(filename);
            
            // 转化成时间戳
            try {
                double timestamp = extractTimestamp(filename_without_extension);
                fileTimestampPairs.emplace_back(timestamp, filename_without_extension);
            } catch (const std::runtime_error& e) {
                // 处理提取时间戳失败的情况
                std::cerr << "警告: " << e.what() << std::endl;
            }
        }

        closedir(dir);

        // 按时间戳排序
        std::sort(fileTimestampPairs.begin(), fileTimestampPairs.end(),
            [](const std::pair<double, std::string>& a, const std::pair<double, std::string>& b) {
                return a.first < b.first;
            }
        );

        // 提取排序后的文件名
        std::vector<std::string> sortedFilenames;
        for (const auto& pair : fileTimestampPairs) {
            sortedFilenames.push_back(pair.second);
        }

        return sortedFilenames;
    }

private:
    // 去除文件名中的扩展名的实现
    std::string removeExtension(const std::string& filename) const {
        size_t last_dot = filename.find_last_of('.');
        if (last_dot == std::string::npos) {
            return filename; // 如果没有点,返回原始文件名
        }
        return filename.substr(0, last_dot);
    }

    // 从文件名中提取时间戳的实现
    double extractTimestamp(const std::string& filename) const {
        // 假设文件名是数字形式的时间戳,例如 "1234567890.jpg"
        try {
            return std::stod(filename);
        } catch (const std::invalid_argument& e) {
            throw std::runtime_error("无法从文件名中提取时间戳: " + filename);
        }
    }


    // 目录路径
    std::string directoryPath;
};





int main() {
    try {

        std::string directory_path = "/home/dongdong/2project/0data/NWPU/img"; // 替换为你的目录路径
        PhotoFileProcessor processor(directory_path);
        std::vector<std::string> filenames = processor.getFilenamesSortedByTimestamp();

        // 打印结果
        for (const std::string& filename : filenames) {
            std::cout << filename << std::endl;
        }

    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
    }

    return 0;
}



#endif // PHOTO_FILE_PROCESSOR_H

  

 CMakeLists.txt

 

cmake_minimum_required(VERSION 3.10)

project(ImageUtilExample VERSION 1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# 添加可执行文件
add_executable(ImageUtilExample main.cpp )

# 如果需要,链接其他库

  

 

posted on 2024-08-18 21:08  MKT-porter  阅读(46)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3