Cを使用してディレクトリ内のファイル数を数える

このコードがコンパイルされる保証はありません。また、実際には Linux と BSD とのみ互換性があります:

#include <dirent.h>

...

int file_count = 0;
DIR * dirp;
struct dirent * entry;

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);

readdir を参照 .