日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Linux下遍历指定目录的C++实现

發(fā)布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux下遍历指定目录的C++实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

????????之前在?https://blog.csdn.net/fengbingchun/article/details/51474728?給出了在Windows遍歷指定文件夾的C++實現(xiàn),這里給出在Linux下遍歷目錄的實現(xiàn),Windows和Linux下的實現(xiàn)都是參考了OpenCV 2.x中的實現(xiàn),OpenCV中的用法可參考https://blog.csdn.net/fengbingchun/article/details/42435901?,OpenCV 3.x中將這一部分移除掉了。

????? ? 目前不論是windows、linux還是opencv本身的實現(xiàn),在目錄中嵌套的目錄只能遍歷一層。

????? ? 測試代碼如下:

#include <dirent.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <string>namespace {void Usage(const char* exe)
{fprintf(stderr, "input params error, run this exe as following command line:\n");fprintf(stderr, "\t%s arg1 arg2 arg3\n", exe);fprintf(stderr, "\targ1: specify the directory to traverse\n");fprintf(stderr, "\targ2: type:\n""\t\t0: tarverse all files and all directories in directory;\n""\t\t1: only tarverse all files, don't include directories in directory;\n""\t\t2: only tarverse all directories, don't include files in directory.\n");fprintf(stderr, "\targ3: optional, filter, default is *, which is don't filter. \n");fprintf(stderr, "for example(support relative path), only traverse jpg image:\n");fprintf(stderr, "\t%s ./images 0 .jpg\n", exe);fprintf(stderr, "##### test fail #####\n");
}// 遍歷指定文件夾下的所有文件,不包括指定文件夾內(nèi)的文件夾
std::vector<std::string> GetListFiles(const std::string& path, const std::string& exten = "*")
{std::vector<std::string> list;list.clear();DIR* dp = nullptr;struct dirent* dirp = nullptr;if ((dp = opendir(path.c_str())) == nullptr) {return list;}while ((dirp = readdir(dp)) != nullptr) {if (dirp->d_type == DT_REG) {if (exten.compare("*") == 0)list.emplace_back(static_cast<std::string>(dirp->d_name));elseif (std::string(dirp->d_name).find(exten) != std::string::npos)list.emplace_back(static_cast<std::string>(dirp->d_name));}}closedir(dp);return list;
}// 遍歷指定文件夾下的所有文件夾,不包括指定文件夾下的文件
std::vector<std::string> GetListFolders(const std::string& path, const std::string& exten = "*")
{std::vector<std::string> list;list.clear();DIR* dp = nullptr;struct dirent* dirp = nullptr;if ((dp = opendir(path.c_str())) == nullptr) {return list;}while ((dirp = readdir(dp)) != nullptr) {if (dirp->d_type == DT_DIR && strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0) {if (exten.compare("*") == 0)list.emplace_back(static_cast<std::string>(dirp->d_name));elseif (std::string(dirp->d_name).find(exten) != std::string::npos)list.emplace_back(static_cast<std::string>(dirp->d_name));}}closedir(dp);return list;
}// 遍歷指定文件夾下的所有文件,包括指定文件夾內(nèi)的文件夾
std::vector<std::string> GetListFilesR(const std::string& path, const std::string& exten = "*")
{std::vector<std::string> list = GetListFiles(path, exten);std::vector<std::string> dirs = GetListFolders(path, exten);for (auto it = dirs.cbegin(); it != dirs.cend(); ++it) {std::vector<std::string> cl = GetListFiles(*it, exten);for (auto file : cl) {list.emplace_back(*it + "/" + file);}}return list;
}} // namespaceint main(int argc, char* argv[])
{if (argc < 3 || argc > 4) {Usage(argv[0]);return -1;}int type = atoi(argv[2]);std::string exten = "*";if (argc == 4) exten = std::string(argv[3]);std::vector<std::string> vec;if (type == 0) vec = GetListFilesR(std::string(argv[1]), exten);else if (type == 1) vec = GetListFiles(std::string(argv[1]), exten);else if (type == 2) vec = GetListFolders(std::string(argv[1]), exten);else { Usage(argv[0]); return -1;}fprintf(stdout, "traverse result: files count: %d\n", vec.size());for (auto& file : vec) {fprintf(stderr, "\t%s\n", file.c_str());}fprintf(stdout, "===== test success =====\n");
}

????? ? CMakeLists.txt文件內(nèi)容如下:

PROJECT(samples_cplusplus)
CMAKE_MINIMUM_REQUIRED(VERSION 3.0)# 支持C++11
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2 -std=c11")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -g -Wall -O2 -std=c++11")INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})FILE(GLOB samples ${PROJECT_SOURCE_DIR}/*.cpp)FOREACH (sample ${samples})STRING(REGEX MATCH "[^/]+$" sample_file ${sample})STRING(REPLACE ".cpp" "" sample_basename ${sample_file})ADD_EXECUTABLE(test_${sample_basename} ${sample})TARGET_LINK_LIBRARIES(test_${sample_basename} pthread)
ENDFOREACH()

????? ? build.sh腳本內(nèi)容如下:

#! /bin/bashecho "Note: new create build directory, and executable file in build"
echo ${PWD}
mkdir -p build
cd build
cmake ..
make

????? ? 執(zhí)行過程:將終端定位到Linux_Code_Test/Samples_cplusplus目錄下,執(zhí)行:./build.sh,然后進入到build目錄下,執(zhí)行生成的執(zhí)行文件即可。

????? ? 測試結(jié)果如下:


????? ? GitHub:?https://github.com/fengbingchun/Linux_Code_Test?

總結(jié)

以上是生活随笔為你收集整理的Linux下遍历指定目录的C++实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。