gpt4 book ai didi

c++ - C++ VS 2010 OpenCV 中 FindFirstFile 参数的 Concat 命令行参数

转载 作者:行者123 更新时间:2023-11-28 07:46:28 25 4
gpt4 key购买 nike

有没有人用过FindFirstFile函数扫描过多个相同类型的文件?

int main(int argc, char** argv){
if(argc != 3)
{
cout <<" Usage: Run [dir of images folder] [file format]" << endl;
cout <<" Example: Run \\imgs\\\\ .jpg " << endl;
return 0;
}


WIN32_FIND_DATA FindFileData;
HANDLE hFind;

string dir = argv[1]; // store user input dir
string type = argv[2]; // store user input file type
stringstream sst;
sst << dir << "*" << type;
string folderDir = sst.str();
sst.str("");
cout << "Scanning all " << type << " files in "<< dir << endl;
cout << folderDir << endl;

/* LOADING IMAGES IN FOLDER */

我试过 folderDir.c_str() 而不是 "\imgs\*.jpg"但我无法让它工作;

hFind = FindFirstFile("\imgs\\*.jpg", &FindFileData);   //images must be in .vcxproj dir
if (hFind != INVALID_HANDLE_VALUE){
int i = 0;
do {
char loc[50] = "\imgs\\"; // Obvsly, I couldn't assign argv[1] here
images.push_back(imread(strcat(loc,FindFileData.cFileName))); //pushes images into vector
img_fname[i] = FindFileData.cFileName; // stores file names into string array
cout << "Successfully loaded " << FindFileData.cFileName << endl; //prints loaded file names
i++;
}while(FindNextFile(hFind, &FindFileData));
}

此外,我可以请求帮助将 string dir 分配给 char loc[50] = "\imgs\\"; 吗?如果只是 char loc[50] = dir;有可能...

我在实例化 loc 之后尝试了 strcpy(loc, dir.c_str()); 但它仍然失败。在未知函数中给我一个错误(无法识别或不支持的数组类型)

最佳答案

i think, it should be only one backslash there: 
"imgs\*.jpg" instead of "\imgs\\*.jpg".

this works fine for me ( and gives me the filelist ):

std::vector<std::string> readdir( const char * dmask )
{
std::vector<std::string> vec;
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
if ((hFind = FindFirstFile(dmask, &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
vec.push_back( FindFileData.cFileName );
} while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
return vec;
}

std::vector<std::string> files = readdir("imgs\*.jpg");

关于c++ - C++ VS 2010 OpenCV 中 FindFirstFile 参数的 Concat 命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com