GNU >> C/C++

scandir


參考資訊
1. man

相較於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, 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;
}


返回上一頁