0

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 */

「\imgs\*.jpg」の代わりに folderDir.c_str() を試しましたが、うまくいきません。

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());ましたが、それでも失敗しました。不明な関数でエラー (認識できない、またはサポートされていない配列型) が返されました

4

1 に答える 1

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");
于 2013-02-10T09:25:58.587 に答える