我們可以使用 dirent.h 這個 library 幫助我們偵測特定資料夾底下的檔案數量,
下方的程式碼只需要修改 path
,更換為自己想要偵測的路徑即可。
內容目錄
Sample Code (範例程式碼)
#include <dirent.h>
int file_count = 0;
DIR * dirp;
struct dirent * entry;
path = "/home/ubuntu/Desktop/"
dirp = opendir(path); /* There should be error handling after this */
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_REG) { /* If the entry is a regular file */
file_count++;
}
}
closedir(dirp);
Reference
Counting the number of files in a directory using C
★留個言吧!內容有誤或想要補充也歡迎與我討論!