2017-04-05 44 views
0

我在實施頂端回答這裏的麻煩:How to get list of files with a specific extension in a given folder獲取的文件清單,相同的擴展和處理它們

我試圖讓所有的「.vol」文件的目錄中的argv [2]對我找到的每個文件執行一些批處理。我想將每個文件傳遞給將字符串作爲參數的ParseFile函數。

// return the filenames of all files that have the specified extension 
// in the specified directory and all subdirectories 
vector<string> get_all(const boost::filesystem::path& root, const string& ext, vector<boost::filesystem::path>& ret){ 
    if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return vector<string>(); 

    boost::filesystem::recursive_directory_iterator it(root); 
    boost::filesystem::recursive_directory_iterator endit; 

    while(it != endit) 
    { 
     if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename()); 
     ++it; 
     cout << *it << endl; 
     return *ret; // errors here 
    } 
} 



... main function 


if (batch) { 
    vector<boost::filesystem::path> retVec; 
    vector<boost::filesystem::path> volumeVec = get_all(boost::filesystem::path(string(argv[2])), string(".vol"), retVec); 


// convert volume files in volumeVec to strings and pass to ParseFile 
    ParseFile(volumeFileStrings); 

} 

我有與get_all功能故障,以及如何正確地返回向量。

+0

添加更多細節。 「我遇到了get_all函數的問題,以及如何正確地返回矢量」 - 具體是什麼問題?你有什麼嘗試?你得到了什麼結果?你期望什麼?此外,而不是「......主要功能」,您應該以[SSCCE](http://sscce.org)的形式形成您的問題,以便其他人可以測試並重現您的結果/問題。 –

+0

我收到錯誤:'operator *'(操作數類型是'std :: vector ')沒有匹配返回* ret – user2007843

+0

爲什麼你用'return * ret'返回你的'while'循環的中間部分 – chbchb55

回答

1
從函數的參數

變化return語句vector<boost::filesystem::path>和刪除ret,而是建立在ret像這樣的功能:

vector<boost::filesystem::path> ret; 

然後,你要移動RET,return ret;的return語句,低於while循環,因此它會將所有文件名追加到ret

您的代碼將是這個樣子:

vector<boost::filesystem::path> get_all(const boost::filesystem::path& root, const string& ext){ 
    if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return; 

    boost::filesystem::recursive_directory_iterator it(root); 
    boost::filesystem::recursive_directory_iterator endit; 
    vector<boost::filesystem::path> ret; 
    while(it != endit) 
    { 
     if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename()); 
     ++it; 
     cout << *it << endl; 
    } 
    return ret; 
}