【发布时间】:2016-08-22 03:59:03
【问题描述】:
我试图只计算路径中的目录,但它不起作用。所以,我不想同时给文件和目录编号,我只想要目录。请问你能帮帮我吗? 代码:
int listdir(char *dir) {
struct dirent *dp;
struct stat s;
DIR *fd;
int count = 0;
if ((fd = opendir(dir)) == NULL) {
fprintf(stderr, "listdir: can't open %s\n", dir);
}
while ((dp = readdir(fd)) != NULL) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue;
stat(dp->d_name, &s);
if (S_ISDIR(s.st_mode))
count++;
}
closedir(fd);
return count;
}
【问题讨论】:
-
使用
S_ISREG()来判断目录条目是否为常规文件,否则为目录(或链接)。 -
不行……