Linux c 目录操作函数scandir

头文件:#include  

定义函数:int  scandir(const char *dir, struct dirent **namelist, nt (*select)  (const  struct  dirent *), nt (*compar)  (const struct dirent **, const struct dirent**));

函数说明:scandir()会扫描参数dir指定的目录文件,经由参数select指定的函数来挑选目录结构至参数namelist数组中,最后再调用参数compar指定的函数来排序namelist数组中的目录数据。每次从目录文件中读取一个目录结构后便将此结构传给参数select所指的函数, select函数若不想要将此目录结构复制到namelist数组就返回0,若select为空指针则代表选择所有的目录结构。scandir()会调用qsort()来排序数据,参数compar则为qsort()的参数,若是要排列目录名称字母则可使用alphasort(). 结构dirent定义请参考readdir()

返回值  :成功则返回复制到namelist数组中的数据结构数目,有错误发生则返回-1


struct linux_dirent64 {
	u64		d_ino;   	 /* inode number 索引节点号 */
	s64		d_off;   	 /* offset to this dirent 在目录文件中的偏移 */
	unsigned short	d_reclen;	/* length of this d_name 文件名长 */
	unsigned char	d_type;		/* the type of d_name 文件类型 */
	char		d_name[0];	/* file name (null-terminated) 文件名,最长255字符 */
};

测试代码如下:

example  1

#include 
#include 
#include 

int main(void)
{
    struct dirent **entry_list;
    int count;
    int i;

    count = scandir("/home/book/workspace", &entry_list, 0, alphasort);
    if (count < 0) {
        perror("scandir");
        return EXIT_FAILURE;
    }

    for (i = 0; i < count; i++) {
        struct dirent *entry;

        entry = entry_list[i];
        printf("%s\n", entry->d_name);
        free(entry);
    }
    free(entry_list);

    return 0;
}


example  2

#define _GNU_SOURCE

#include 
#include 
#include 
#include 
#include 

void make_test_data(char *path)
{
    int i;

    for (i = 0; i < 20; i++) {
        char file[128];

        sprintf(file, "file-%d", i);
        creat(file, 0);
    }
}

void scan(char *path, char *title, int (*sort)(const void *a, const void *b))
{
    struct dirent **entry_list;
    int count;
    int i;

    puts(title);
    count = scandir(path, &entry_list, 0, sort);
    if (count < 0) {
        perror("scandir");
        return EXIT_FAILURE;
    }

    for (i = 0; i < count; i++) {
        struct dirent *entry;

        entry = entry_list[i];
        printf("%s\n", entry->d_name);
        free(entry);
    }

    printf("\n");
    free(entry_list);
}

int main(void)
{
    char *path = ".";

    make_test_data(path);
    scan(path, "alphasort: ", alphasort);
    scan(path, "versionsort: ", versionsort);
    return 0;
}

example  3

#include 
#include 
#include 

int filter(const struct dirent *entry)
{
    return entry->d_name[0] == 'a';
}

int main(void)
{
    struct dirent **entry_list;
    int count;
    int i;

    count = scandir("/usr/include", &entry_list, filter, alphasort);
    if (count < 0) {
        perror("scandir");
        return EXIT_FAILURE;
    }

    for (i = 0; i < count; i++) {
        struct dirent *entry;

        entry = entry_list[i];
        printf("%s\n", entry->d_name);
        free(entry);
    }
    free(entry_list);

    return 0;
}


查看 entry->d_name是目录还文件参考这里:点击这里!





你可能感兴趣的:(Linux,note)