我是新的C++。有人可以請給我一些代碼如何獲得所有目錄和所有它的子目錄在LINUX中RECURSIVELY。我在互聯網上沒有找到任何可以幫助我的東西(或者可以工作的代碼)。我需要使用文件夾和它的子文件夾來獲取所有文件。C++列出中的所有目錄和子目錄(LINUX)
在Ubuntu我沒有的GetFiles,目錄...
我是新的C++。有人可以請給我一些代碼如何獲得所有目錄和所有它的子目錄在LINUX中RECURSIVELY。我在互聯網上沒有找到任何可以幫助我的東西(或者可以工作的代碼)。我需要使用文件夾和它的子文件夾來獲取所有文件。C++列出中的所有目錄和子目錄(LINUX)
在Ubuntu我沒有的GetFiles,目錄...
的Windows:http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx
的Unix/Linux:http://www.linuxquestions.org/questions/programming-9/c-list-files-in-directory-379323
遞歸應用您應用到頂級目錄相同的算法。
另外的Dmitri's answer,你可能會感興趣的使用nftw庫函數「做遞歸你」
的答案上列出目錄只是答案的第一部分,但我沒有看到任何人回答遞歸部分。要遞歸地做任何事情你必須創建一個「調用自己」的子例程 - 注意在處理符號鏈接時應該小心,特別是在使用nfs時/ export等情況下,這可能導致循環遞歸併將你鎖定在無限循環!基本上是:
這不是真正的代碼,它的僞代碼,試圖幫助你的遞歸是如何工作的一個更好的想法 沒有這個想法混淆你的語言可以 在具有某種語言的任何應用其中 這幾天幾乎所有的語言我能想到
// PSEUDO-CODE
stiring entriesarray[] myfunc(string basedir)
{
string results[] = getfilesandfolders(basedir) // you might want to specify a filter
for each string entry in results
{
// if you want only files you'll need to test for if entry is a file
entriesarray.add(entry)
if (entry is a directory && entries is not a symbolic link)
{
string moreentriesarray[] = myfunc(entry)
entriesarray.join(moreentriesarray)
}
}
return entriesarray[]
}
通知如何,如果項目不包含任何實際的目錄,該函數不調用自身調用返回的機制?這很重要,因爲這是如何避免無限遞歸。不過要注意的是,您可能想要使其可以取消此操作,但現在較大的文件系統 可能需要很長時間才能處理。我通常這樣做的方式是啓動另一個 線程並在後臺執行搜索,並讓後臺線程檢查 取消標誌,以防止用戶想要停止操作,因此可以發佈有關多少信息剩餘時間,完成百分比等等。它有點粗糙,但這應該讓任何人對這種類型的東西感興趣,走向正確的方向。記得要始終正確地檢查錯誤並進行異常處理,這是 ,我看到新的程序員跳過所有的時間。
試試這個在Linux上:
#include <iostream>
#include <string>
#include <dirent.h>
void ProcessDirectory(std::string directory);
void ProcessFile(std::string file);
void ProcessEntity(struct dirent* entity);
std::string path = "/path/to/directory/";
int main()
{
std::string directory = "theDirectoryYouWant";
ProcessDirectory(directory);
return 0;
}
void ProcessDirectory(std::string directory)
{
std::string dirToOpen = path + directory;
auto dir = opendir(dirToOpen.c_str());
//set the new path for the content of the directory
path = dirToOpen + "/";
std::cout << "Process directory: " << dirToOpen.c_str() << std::endl;
if(NULL == dir)
{
std::cout << "could not open directory: " << dirToOpen.c_str() << std::endl;
return;
}
auto entity = readdir(dir);
while(entity != NULL)
{
ProcessEntity(entity);
entity = readdir(dir);
}
//we finished with the directory so remove it from the path
path.resize(path.length() - 1 - directory.length());
closedir(dir);
}
void ProcessEntity(struct dirent* entity)
{
//find entity type
if(entity->d_type == DT_DIR)
{//it's an direcotry
//don't process the '..' and the '.' directories
if(entity->d_name[0] == '.')
{
return;
}
//it's an directory so process it
ProcessDirectory(std::string(entity->d_name));
return;
}
if(entity->d_type == DT_REG)
{//regular file
ProcessFile(std::string(entity->d_name));
return;
}
//there are some other types
//read here http://linux.die.net/man/3/readdir
std::cout << "Not a file or directory: " << entity->d_name << std::endl;
}
void ProcessFile(std::string file)
{
std::cout << "Process file : " << fileToOpen.c_str() << std::endl;
//if you want to do something with the file add your code here
}
使用nftw
。它提供了各種選項來微調目錄遍歷。
該頁面還顯示an example。
遞歸是不必要的。 Linux上有一個工具可以迭代執行此操作。
#include <ftw.h>
int ftw(const char *dirpath,
int (*fn) (const char *fpath, const struct stat *sb,
int typeflag),
int nopenfd);
的ftw()
功能要求在給定的樹中的每個文件和目錄提供的回調函數。