2013-02-10 101 views
0

是否有人使用FindFirstFile函数来扫描同一类型的多个文件?C++中的FindFirstFile参数的Concat命令行参数VS 2010 OpenCV

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 dirchar loc[50] = "\imgs\\";帮助? 如果只有char loc [50] = dir;是可能的...

我试过strcpy(loc, dir.c_str());实例化loc后,但它仍然失败。给我一个错误(无法识别或不支持阵列类型)在未知功能

回答

1
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"); 
+0

嗨,Berak!谢谢,但我的问题是我如何通过命令行arg从用户的输入中创建目录。 – Masochist 2013-02-10 10:19:40

+1

'std :: string dirmask = argv [1]; dirmask + =“\ *。jpeg”; std :: vector files = readdir(dirmask.c_str());' – berak 2013-02-10 10:28:21

+0

dirmask.c_str()说找不到指定的文件:/ – Masochist 2013-02-10 10:56:47