我想获得文件夹中的文件以外的目录数量,但我无法获得正确的结果。有人帮我解决这个问题?特别是我应该发送到isDirectory()函数?如何获取目录中的目录数量?
int listFilesIndir(char *currDir)
{
struct dirent *direntp;
DIR *dirp;
int x ,y =0 ;
if ((dirp = opendir(currDir)) == NULL)
{
perror ("Failed to open directory");
return 1;
}
while ((direntp = readdir(dirp)) != NULL)
{
printf("%s\n", direntp->d_name);
x= isDirectory(dirp);
if(x != 0)
y++;
}
printf("direc Num : %d\n",y);
while ((closedir(dirp) == -1) && (errno == EINTR)) ;
return 0;
}
int isDirectory(char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) == -1)
return 0;
else
return S_ISDIR(statbuf.st_mode);
}
你会得到什么而不是“正确的结果”? – 2013-04-01 20:37:44
将“dirp”传递给isDirectory()似乎是错误的。这是一个错字吗? – kkrambo
这个评论可能是完全没有帮助的,但它让我的眼睛看到如此多的代码行用于概念化的单行代码...... ls -d/*/| wc -l' –
shx2