cwindowsfiledirent.h

using ((entry = readdir(folder)) != NULL) in multiple loops C


I am working on directories using dirent.h in c and I want to use a couple of loop with ((entry = readdir(folder)) != NULL) as a condition.

my question is, do I need to somehow reset it to the starting point of he directory again after the loop, and if so, how?

for example :


while ((entry = readdir(folder)) != NULL)
{
    //...
}

//and then, use another loop with the same condition

while ((entry = readdir(folder)) != NULL)
{
    //...
}

Solution

  • To restart the directory enumeration, you can either:

    Note however these remarks:

    Modified pseudo code:

        while ((entry = readdir(folder)) != NULL)
        {
            //...
        }
        
        //and then, use another loop with the same condition
        rewinddir(folder);
        
        while ((entry = readdir(folder)) != NULL)
        {
            //...
        }