C++ 从文件夹c+;读取所有文件+;

C++ 从文件夹c+;读取所有文件+;,c++,file,directory,C++,File,Directory,我有一个从文件夹中读取图像的程序。我使用for访问文件的所有索引并将其存储到向量中: for(int i=0; i<labels.size(); i++){ ostringstream stringStream; stringStream << setfill ('0') << setw (4) << i; num2string = stringStream.str(); string img = "C:\\o

我有一个从文件夹中读取图像的程序。我使用for访问文件的所有索引并将其存储到向量中:

    for(int i=0; i<labels.size(); i++){

    ostringstream stringStream;
    stringStream << setfill ('0') << setw (4) << i;
    num2string = stringStream.str();

    string img = "C:\\opencvAssets/detected/BioID_"+num2string+".pgm";
    //cout<< img <<" \n";
    images.push_back(imread(img, CV_LOAD_IMAGE_GRAYSCALE));  //labels.push_back(i);
}

for(int i=0;i首先需要扫描目录并获取文件:

您可以使用和

bool find_files(){
WIN32_FIND_DATA FindFileData;
string img=“C:\\opencvAssets/detected/BioID.*.pgm”;
HANDLE hFind=FindFirstFile(img.c_str(),&FindFileData);
if(hFind==无效的句柄值){
返回false;
} 
否则会{

cout与SHR的示例一样,您需要扫描目录并获取文件。 您可以在每个Unix平台上使用特定于Windows的实现或中的函数

有关Unix上dirent.h的更多信息,请参阅。

补丁:

if (Cv::mat m = imread(img, CV_LOAD_IMAGE_GRAYSCALE)) images.push_back(m); 

但对于严肃的任务,请使用boost::filesystem限制对实际存在的文件的访问。

在Linux上,您可以这样做:

1) 创建一个DIR指针, 使用opendir()打开目录

DIR*ptr=opendir(目录的路径)

2) 创建struct dirent指针, 使用readdir()从目录中读取文件

struct dirent*ptr=readdir(ptr);//传递DIR指针

3) 在while循环中运行上述操作。将向量中的数据推回,作为引用传递给此函数或返回向量

4) 确保“.”和“.”不是一个文件,所以不要在向量中推送它。 //要检查这一点,可以使用std::strcmp(dirent\u pointer->d\u name,“.”=0 所以..如果(!std::strcmp(ptr->d_name,“.”==0)


希望对您有所帮助

您可以使用boost::filesystem。但是,这不是一个只包含头的库,您可能需要不与外部库链接,或者这样做可能非常不方便。 在Windows上(看起来像是这样),我喜欢使用这个类来获取与给定模式匹配的所有文件名

#pragma once
#include <string>
#include <vector>
#include <windows.h>

#pragma comment(lib, "User32.lib")

#undef tstring
#undef tcout
#if defined(_UNICODE) || defined(UNICODE)
#define tstring std::wstring
#define tcout std::wcout
#else
#define tstring std::string
#define tcout std::cout
#endif

class FileFinder {
  WIN32_FIND_DATA ffd;
  HANDLE _handle;

public:
  FileFinder(LPCTSTR pattern) { _handle = FindFirstFile(pattern, &ffd); }
  ~FileFinder() { FindClose(_handle); }
  const TCHAR *FindFirst() const {
    return _handle != INVALID_HANDLE_VALUE ? ffd.cFileName : nullptr;
  }
  const TCHAR *FindNext() {
    return FindNextFile(_handle, &ffd) ? ffd.cFileName : nullptr;
  }
  std::vector<tstring> GetAllNames() {
    std::vector<tstring> result;
    for (auto name = FindFirst(); name; name = FindNext())
      result.push_back(name);
    return result;
  }
};
#pragma一次
#包括
#包括
#包括
#pragma注释(lib,“User32.lib”)
#未定义字符串
#未定义输出
#如果已定义(_UNICODE)| |已定义(UNICODE)
#定义tstring std::wstring
#定义tcout std::wcout
#否则
#定义tstring std::string
#定义tcout std::cout
#恩迪夫
类文件查找器{
WIN32_查找_数据ffd;
把手(HANDLE);
公众:
FileFinder(LPCTSTR模式){u handle=FindFirstFile(模式,&ffd);}
~FileFinder(){FindClose(_handle);}
常量TCHAR*FindFirst()常量{
return _handle!=无效的_handle_值?ffd.cFileName:nullptr;
}
常量TCHAR*FindNext(){
返回FindNextFile(_handle,&ffd)?ffd.cFileName:nullptr;
}
std::vector GetAllNames(){
std::向量结果;
对于(自动名称=FindFirst();名称;名称=FindNext())
结果。推回(名称);
返回结果;
}
};
它遵循RAII范式,不会因异常而泄漏资源。 它的用法示例如下

#include <tchar.h>
#include <iostream>
#include "FileFinder.h"

int _tmain(int argc, TCHAR *argv[]) {
  DWORD dwError = 0;

  if (argc != 2) {
    _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
    return -1;
  }

  tstring pattern(argv[1]);
  pattern.erase(pattern.find_last_not_of(TEXT("\\")) + 1);
  pattern += TEXT("\\*.pgm");
  if (pattern.length() > MAX_PATH) {
    _tprintf(TEXT("\nDirectory path is too long.\n"));
    return -1;
  }
  FileFinder finder(pattern.c_str());
  auto files = finder.GetAllNames();
  for (const auto &f : files)
    tcout << f << std::endl;
  return 0;
}
#包括
#包括
#包括“FileFinder.h”
int_tmain(int argc,TCHAR*argv[]){
DWORD dwError=0;
如果(argc!=2){
_tprintf(文本(“\n用法:%s\n”),argv[0]);
返回-1;
}
t串模式(argv[1]);
模式。擦除(模式。查找(文本(“\\”)+1的最后一个);
模式+=文本(“\\*.pgm”);
if(pattern.length()>最大路径){
_tprintf(文本(“\n目录路径太长。\n”);
返回-1;
}
FileFinder(pattern.c_str());
自动文件=finder.GetAllNames();
用于(常量自动&f:文件)

t但是,使用标准库无法获取目录的内容。但是,我怀疑boost可能提供了一个抽象。您想检查具有给定字符串名称的文件是否存在,对吗?检查这个,或者如果您有权访问boost,请使用文件系统库中的目录迭代器。我有e同样的问题,并用dirent和fnmatch解决了。这只适用于Windows。如果你在其他平台上不需要它,这没关系。是的,但他给了一个Windows路径作为常量,所以我不敢相信他想要其他操作系统。我真的想要win和linux!!路径只是一个例子!!我正在尝试在中使用这种方法,使用exists function..bool exists(const std::string&name)。我对调用函数是如何存在的有点困惑。它必须是函数的参数吗?我不会为此使用fork()。最好检查标准文件的类型:dirent\u pointer->d\u type==DT\u REG
#include <tchar.h>
#include <iostream>
#include "FileFinder.h"

int _tmain(int argc, TCHAR *argv[]) {
  DWORD dwError = 0;

  if (argc != 2) {
    _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
    return -1;
  }

  tstring pattern(argv[1]);
  pattern.erase(pattern.find_last_not_of(TEXT("\\")) + 1);
  pattern += TEXT("\\*.pgm");
  if (pattern.length() > MAX_PATH) {
    _tprintf(TEXT("\nDirectory path is too long.\n"));
    return -1;
  }
  FileFinder finder(pattern.c_str());
  auto files = finder.GetAllNames();
  for (const auto &f : files)
    tcout << f << std::endl;
  return 0;
}