1

I have to find how many folders and how many regular files in a directory. I've tried to something but I couldn't even compile my code. I don't even know my program is right or not.When I tried to compile my code there are 2 type of error. One of them is error: struct dirent has no member named 'd_type' and 'DT_DIR' undeclared(first use in this function, DT_REG undeclared(first use in this function).

I'm using CodeBlocks with MinGW, if the error is about the my compiler; I have to use this IDE.

How can I fix my code?

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    int file_count = 0;
    int dir_count = 0;
    struct dirent * entry;
    DIR *dp;

    if (argc != 2)
    {
        printf("usage: give directory_name\n");
        exit(-1);
    }

    if ((dp = opendir(argv[1])) == NULL)
    {
        printf("Error: can't open %s\n", argv[1]);
        exit(-2);
    }
    while ((entry= readdir(dp)) != NULL){

        if (entry->d_type == DT_REG)
         file_count++;

        else if (entry->d_type == DT_DIR)
         dir_count++;
    }

    closedir(dp);

    printf(" %d Number of file ", file_count);
    printf(" %d Number of folders", dir_count);
    exit(0);
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
megeren
  • 11
  • 2
  • I don't know if this is what causes your problem but it looks like we are missing some of your `#include` directives at the beginning of your probem. Could you check your code and edit your question? Thank you! – Patrick Jun 17 '20 at 16:43
  • 1
    Actually there is no mistake about include libraries. I did them all. #include ,, , , But They don't apper in here I Made a mistake while I'm posting my code. – megeren Jun 17 '20 at 16:50

1 Answers1

1

Maybe you're missing some includes?

Here is my version

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

int main(int argc, char *argv[]) {
    DIR *dir;
    struct dirent *ent;

    size_t nfiles = 0, ndirs = 0;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s directory\n", argv[0]);
        return -1;
    }

    if (!(dir = opendir(argv[1]))) {
        fprintf(stderr, "[!] Could not open directory `%s': %s\n", argv[1],
                strerror(errno));
        return -2;
    }

    while ((ent = readdir(dir))) {
        // Ignore . and .. entries
        if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
            continue;
        }

        if (ent->d_type == DT_REG) {
            ++nfiles;
        } else if (ent->d_type == DT_DIR) {
            ++ndirs;
        }
    }

    closedir(dir);
    printf("%lu Files, %lu Directories\n", nfiles, ndirs);

    return 0;
}
  • if (ent->d_type == DT_REG) { This line giving : 'struct dirent' has no member named 'd_type' and DT_REG was not declared in this scope. } else if (ent->d_type == DT_DIR) { This line giving: 'struct dirent' has no member named 'd_type' and DT_DIR was not declared in this scope. – megeren Jun 17 '20 at 17:11
  • What is your OS and compiler? – Ayman Al-Qadhi Jun 17 '20 at 17:12
  • My Os is Windows. My compiler is CodeBlocks but I've also tried on DevC++. – megeren Jun 17 '20 at 17:15
  • The functions your trying to use are POSIX functions. They may not be available in non posix-compliant operating systems like windows.. – Ayman Al-Qadhi Jun 17 '20 at 17:19
  • I wanted to ask another code to you. How can I do that? – megeren Jun 17 '20 at 17:42
  • Check this question: https://stackoverflow.com/questions/2314542/listing-directory-contents-using-c-and-windows – Ayman Al-Qadhi Jun 17 '20 at 17:50