EggHead
EggHead

Reputation: 121

C - OpenDir() Number of Entries

After getting a DIR * using opendir(), I need to use readdir() to read and store the struct dirent into an array.

In order to figure out the size of the array, I could just loop through and count the entries. Then, I could allocate the array and then loop through again to read and store the struct dirent.

However, I'm wondering whether there is a better way to get the number of dir entries?

Upvotes: 2

Views: 650

Answers (1)

tad
tad

Reputation: 506

The realloc way is probably the best way to go. Here's an example (small allocation size chosen for demo purposes.) No error checking performed. At the end of the loop, direntArray has the goods, and count tells you how many there are.

#define num_to_alloc 10

int main(int argc, const char * argv[])
{

    struct dirent *direntArray = NULL;

    DIR *myDir = opendir("/tmp");
    int count = 0;
    int max = 0;
    struct dirent *myEnt;

    while ((myEnt = readdir(myDir))){
        if ( count == max ){
            max += num_to_alloc;
            direntArray = realloc(direntArray, max * sizeof(struct dirent));
        }
        memcpy(&direntArray[count], myEnt, sizeof(struct dirent));
        count++;
    }
    return 0; 
}

Upvotes: 1

Related Questions