简体   繁体   English

如何获取目录中的目录数?

[英]how can I get the number of directories in a directory?

I am trying to get the number of directories in a folder except the files in but I cannot get the correct result. 我试图获取除文件夹之外的文件夹中的目录数,但我无法得到正确的结果。 Somebody help me to solve this problem? 有人帮我解决了这个问题吗? Especially what should I sent to the isDirectory() function? 特别是我应该发送到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);
}

You're sending a directory stream to the function, and treating it like a path. 您正在向该函数发送目录流,并将其视为路径。

Linux and some other Unix systems include a way to get this info directly: Linux和其他一些Unix系统包含一种直接获取此信息的方法:

while ((direntp = readdir(dirp)) != NULL)
{
    printf("%s\n", direntp->d_name);
    if (direntp->d_type == DT_DIR)
       y++;
}

Otherwise, make sure you send the right details to the function, ie 否则,请确保将正确的详细信息发送到该功能,即

x= isDirectory(direntp->d_name);

The call for your function is wrong. 你的功能调用是错误的。

x= isDirectory(dirp);

While the prototype of function is: 而功能的原型是:

int isDirectory(char *path) 

It need a string as parameter, but you give it a "DIR *dirp;". 它需要一个字符串作为参数,但你给它一个“DIR * dirp;”。 I changed the code as: 我将代码更改为:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

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);
        if(direntp->d_type == DT_DIR)
            y++;
    }
    printf("direc Num : %d\n",y );

    while ((closedir(dirp) == -1) && (errno == EINTR)) ;

    return 0;
}

int main(int argc, char **argv){
    if(argc == 2){
        // Check whether the argv[1] is a directory firstly.
        listFilesIndir(argv[1]);
    }
    else{
        printf("Usage: %s directory", argv[0]);        
    }
    return 0;
}

I tested it on my Linux server. 我在我的Linux服务器上测试过它。 And it works well. 它运作良好。 SO @teppic is right. 所以@teppic是对的。 But pay attention, in the code, the number of directory includes two specific ".." (parent directory) and "." 但要注意,在代码中,目录的数量包括两个特定的“..”(父目录)和“。”。 (current directory). (当前目录)。 If you do not want to include it, you could change: 如果您不想包含它,您可以更改:

printf("direc Num : %d\n",y );

into: 成:

printf("direc Num : %d\n",y-2 );

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM