1

I'm doing a function that counts the number of files in a directory (including subdirectories and files inside that subdirectory). For example:

Base/Dir 1/Dir 1.1

Dir 1 has: hi.txt, hi2.txt

Dir 1.1 has: hi3.txt, hi4.txt, hi5.txt

So the output for number of files in Base should be 7 (ignoring . and ..)

The output for number of files in Dir 1 should be 6

The output for number of files in Dir 2 should be 3

This is what i've tried to do.

void recorrido(const char *actual, int indent, int op, char * output,int numF)
{
    DIR *dir;
    struct dirent *entrada;


    char path[PATH_MAX+1];
    char path2[PATH_MAX+1];

    if (!(dir = opendir(actual))){
        return;
    }


    while ((entrada = readdir(dir)) != NULL)
    {

        if (entrada->d_type == DT_DIR) //Directory
        {
            if ((strcmp(entrada->d_name, ".") != 0) && (strcmp(entrada->d_name, "..") != 0)) //Ignore . and ..
            {

                strcpy(path, actual);
                strcat(path, "/");
                strcat(path, entrada->d_name);


                recorrido(path, indent + 2,op,output,numF++);
                printf("Number of files for %s is %d", path, numF);
            }
        }
    }

 else if (entrada->d_type != DT_DIR){ //just file

            if (strcmp(actual, "") == 0){
                strcpy(path2, "./"); 
                strcat(path2, entrada->d_name);
                strcpy(actual, path2);
            }
            else
            {
                strcpy(path2, actual);
                strcat(path2, "/");
                strcat(path2, entrada->d_name);
                //printf("File path is %s\n",path2);
                numF++;
            }
        }


    closedir(dir);

}

I have issues printing the proper number of files for each directory, if I have 2 folders inside of base (test 1 and test 2) it will take in account those folders but if I have something inside test 1 it will ignore it.

5
  • Why does this function return void? Commented Jul 6, 2019 at 14:44
  • numF++ increments the local variable. It does not affect the caller's variable. Commented Jul 6, 2019 at 14:44
  • @wildplasser I just want to print the number of files in each directory so I didn't see necessary for it to return something
    – pinkWojak
    Commented Jul 6, 2019 at 14:45
  • pinkWojak, How do you expect the 3 from Dir 1.1 to affect the output of Dir 1? Commented Jul 6, 2019 at 15:23
  • how could i fix it?
    – pinkWojak
    Commented Jul 6, 2019 at 15:26

1 Answer 1

1

As noted in the comments, you need to return the incremented value.

  1. Change the function signature:

    int recorrido(const char *actual, int indent, int op, char *output, int numF)
    
  2. Change how the function calls itself:

    numF = recorrido(path, indent + 2, op, output, numF + 1);
    
  3. Return the modified value:

    …
    if (! (dir = opendir(actual))) {
        return numF;
    }
    …
    
        …
        closedir(dir);
        return numF;
    }
    
  4. … and change how the function is called.

I also strongly suggest not mixing language (stick to English for code and comments!), and to spend time formatting your code cleanly and consistently (especially indentation and spacing). The readers of your code (including yourself!) will thank you — in fact, cleanly formatted code isn’t optional, it’s enforced without exception virtually everywhere.

Not the answer you're looking for? Browse other questions tagged or ask your own question.