Click here to Skip to main content
15,891,880 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
int listdir ( char *basePath, char files[][SIZE], int *total){
    char path[PATH_MAX];
    struct dirent *dp;
    DIR *dir;
    unsigned count=0;

    if ( NULL != ( dir = opendir ( basePath))) {
        while ( NULL != ( dp = readdir(dir))) {
           if (strcmp ( dp->d_name, ".") != 0 && strcmp ( dp->d_name, "..") != 0){
                strcpy ( path, basePath);
                strcat ( path, "/");
                strcat ( path, dp->d_name);
                if ( dp->d_type == DT_DIR) {
                    if ( *total < ELEMENTS) {
                        snprintf (files[*total], SIZE, "%s", path);
                        files[*total][SIZE - 1] = 0;
                        ++(*total);
                        continue;
                        count++;
                    }
                }
                if ( dp->d_type == DT_DIR) {
                    listdir ( path, files, total);//recursive call
                }
            }
        }
        printf("Counted: %u subdirectories.\n", count);
        closedir(dir);
    }
    else {
        perror (basePath);//opendir failed
    }
}


What I have tried:

In this code, I am trying to count the number of subdirectories. I used continue and count but when I run this code, it shows me this result.
Given result

<big>Counted: 0 subdirectories.</big>
But when I remove the continue and run the code, the result shows me this

Given result again

<big>Counted: 0 subdirectories.
Counted: 0 subdirectories.
Counted: 1 subdirectories.
Counted: 0 subdirectories.
Counted: 0 subdirectories.
Counted: 1 subdirectories.
Counted: 4 subdirectories.</big>
I want to actually count the number of subdirectories. How to do this?
Posted
Updated 22-Feb-21 23:09pm
v2
Comments
Rick York 23-Feb-21 13:43pm    
I recommend that you increase your compiler's warning level. This logic flaw would be caught and flagged by most compilers if the warning level is set high enough. I don't know exactly what level will catch that one because I ALWAYS use level 4.

In C (and all C-like languages) the continue keyword means "stop what you are doing and go around the nearest loop again".

Any code after the line containing the continue instruction will never be executed - and most compilers generate either a warning or an error to tell you the "unreachable code" has been detected: ignore these at your peril! If it's a warning, it's serious, the compiler does know more than you (particularly when you are just beginning) and you should treat warnings as errors.
If it's an error, then it will prevent an executable file being produced, so whatever you execute after that compilation will not include any changes since your last sucessfull, error-free compile.

So in your code, count is never increased:
C++
files[*total][SIZE - 1] = 0;
++(*total);
continue;
count++;

For the rest of you problem, you only check for a directory and count it if a parameter value is less than what I presume is a constant value - and since we have no idea what either of those are, we can't help.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Try this:
C++
void listdir ( char *basePath, char files[][SIZE], int *total){
    char path[PATH_MAX];
    struct dirent *dp;
    DIR *dir;
    unsigned count=0;

    if ( NULL != ( dir = opendir ( basePath)))
    {
    //    printf("Checking: %s\n", basePath);
        while ( NULL != ( dp = readdir(dir)))
        {
            if ( dp->d_type == DT_DIR)
            {
                if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
                {
                    continue; // skip 'this' and parent entries
                }
                count++;
                sprintf(path, "%s/%s", basePath, dp->d_name);
                listdir (path, files, total);//recursive call
// not sure what these lines are supposed to do
//                if ( *total < ELEMENTS) {
//                    snprintf (files[*total], SIZE, "%s", path);
//                    files[*total][SIZE - 1] = 0;
//                    ++(*total);
//                }
            }
        }
        printf("Counted: %u subdirectories in %s\n", count, basePath);
        closedir(dir);
    }
    else {
        perror (basePath);//opendir failed
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900