1
A
回答
1
E.g.沒有錯誤檢查:
struct stat buffer;
int status;
char path[PATH_MAX];
DIR *dir = opendir(dir_name);
...
struct dirent *de = readdir(dir);
sprintf(path, "%s/%s", dir_name, de->d_name);
status = lstat(path, &buffer);
if(S_ISDIR(buffer.st_mode))
{
...
}
編輯:修復包括目錄在lstat路徑(每el.pescado)。正如R Samuel Klatchko所指出的那樣,您可能希望採取白名單方式(S_ISREG),而不是在列入黑名單類型時。
0
`void DirectryNFileCount(const char * FileDir)
{
DIR *dir;
int filecount;
int dircount;
struct dirent *direntry;
if ((dir = opendir (FileDir)) == NULL)
{
/*Error code*/
}
while((direntry = readdir (dir)) != NULL)
{
if(direntry->d_type==DT_DIR)
dircount++;
/*do something with directries */
}
else
{
filecount++;
std::cout<<"Files Names"<<direntry->d_name<<std::endl;
}
}
std::cout<<"THIS Directory has "<<filecount<<" FILES and "<<dircount<< " DIRECTORIES";
}
+1
伴隨您的代碼的書面說明會使此答案更有幫助。 – nbryans 2016-11-18 20:56:51
相關問題
- 1. 如何忽略Perl的readdir中的單點和雙點條目?
- 2. Git:我如何忽略子目錄中的匹配目錄?
- 3. .htaccess如何忽略目錄?
- 4. 如何忽略目錄
- 5. 如何忽略git中的目錄
- 6. 忽略os.walk()中的目錄?
- 7. 忽略eclipse中的目錄
- 8. 忽略SVN中的目錄
- 9. SVN忽略 - 如何忽略目錄的內容
- 10. 獲取混帳不忽略通配符後的目錄忽略
- 11. 如何忽略Composer的目錄?
- 12. Git:如何忽略隱藏的目錄?
- 13. 如何獲得parseDouble忽略 「%」 - Java的
- 14. CVS忽略目錄
- 15. Htaccess忽略目錄
- 16. .htaccess忽略目錄
- 17. 忽略git目錄
- 18. Excel VBA中忽略目錄
- 19. GIT:忽略某些目錄,但不忽略其他目錄
- 20. 如何忽略一個目錄及其子目錄中gitignore
- 21. collectstatic如何忽略子目錄?
- 22. 如何使用RecursiveIteratorIterator忽略目錄?
- 23. 如何讓dirent忽略當前目錄?
- 24. 如何忽略子目錄ASP.NET 4.0
- 25. SVN如何忽略一個目錄
- 26. gitignore - 如何忽略.idea /目錄爲rubymine
- 27. 忽略子目錄的.gitignore
- 28. 如何獲得mysql_errno()爲INSERT忽略
- 29. 獲得目錄忽略文件擴展名
- 30. 如何在魷魚2.7中使用3.1的功能「忽略-cc」
你必須在傳遞路徑到lstat之前結合dirname和filename,例如。做sprintf(路徑,「%s /%s」,dir_name,de-> d_name); – 2010-04-16 22:42:11
只處理常規文件條目('S_ISREG')而不排除目錄條目('S_ISDIR')會更健壯一些。這樣,如果您遇到另一種類型的條目,則不會意外處理它。 – 2010-04-16 22:44:16
剛剛注意到別的東西。你需要考慮是否使用'stat'或'lstat'。如果你有一個符號鏈接到一個真實的文件,你想要處理它還是跳過它?如果你想跳過它,使用'lstat',這樣你就會知道它是一個符號鏈接。如果你不關心它是一個符號鏈接,並且想要處理它,假設它是一個常規文件的符號鏈接,那麼使用'stat'。 – 2010-04-17 06:11:52