參考資訊
https://man7.org/linux/man-pages/man3/scandir.3.html
相較於readdir(),scandir()提供排序功能,使用者可以使用alphasort或者自定義的排序callback
main.c
#define _DEFAULT_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
int c = 0;
int n = 0;
struct dirent **namelist = NULL;
n = scandir("/tmp", &namelist, NULL, alphasort);
if (n == -1) {
return -1;
}
for (c = 0; c < n; c++) {
printf("%s\n", namelist[c]->d_name);
free(namelist[c]);
}
free(namelist);
return 0;
}