Ok, this question won't help me. The answers only print each file in a given directory. That's not enough. I need to actually record a list, in a struct dirent **
. I have:
struct dirent **Entries;
DIR *Dir = opendir("/path/to/directory");
How would I record each entry into the Entries
list, without calling realloc each iteration with a while loop. I need a faster way to record a list of items in a directory. Is there a faster way other than:
struct dirent **Entries;
DIR *Dir = opendir("/path/to/directory");
struct dirent *Entry;
unsigned int Count = 0;
while ((Entry = readdir(Dir))) {
Count++;
}
Entries = malloc(Count*sizeof(char*));
closedir(Dir);
Dir = opendir("Some dir path"); //Also, is there a better way to reset it, other than closing it and reopening?
for (int i = 0; (Entry = readdir(Dir); i++) {
Entries[i] = Entry;
}
Or
struct dirent **Entries;
DIR *Dir = opendir("/path/to/directory");
Dir = opendir("Some dir path");
struct dirent *Entry;
for (int i = 0; (Entry = readdir(Dir); i++) {
Entries = realloc(Entries, i*sizeof(struct dirent*));
Entries[i] = Entry;
}
closedir(Dir);
Like, is there a direct way to get all the contents of a directory at once, or a better way to handle a growing buffer of entries?
Help? Please?