#include <dirent.h> int scandir(const char *dirp, struct dirent ***namelist,
int alphasort(const void *a, const void *b); int versionsort(const void *a, const void *b);
glibc 需要特性测试宏(参看 feature_test_macros(7)):
scandir()、alphasort():_BSD_SOURCE || _SVID_SOURCE
versionsort():_GNU_SOURCE
alpthasort() 和 versionsort() 可以用作比较函数 compar()。前者通过strcoll(3) 来排序项目,后者使用字符串字段(*a)->d_name 和(*b)->d_name 上应用 strverscmp(3)。
alphasort() 和 versionsort() 函数返回一个小于、等于或大于零的整数,当第一个参数被认为是小于、等于或大于第二个参数时。
函数 scandir() 和 alphasort() 来自 4.3BSD,并且在 Linux libc4 里已经存在。libc4 和 libc5 使用更安全的原型:
int alphasort(const struct dirent ** a, const struct dirent **b);
但 glibc 2.0 使用不太精确的 BSD 原型。
函数 versionsort() 是 GNU 扩展,从 glibc 2.1 开始有效。
从 glibc 2.1 开始,alphasort() 调用 strcoll(3);之前的调用strcmp(3)。
#define _SVID_SOURCE /* 以反序打印当前目录里的文件名 */ #include <dirent.h> int main(void) { struct dirent **namelist; int n; n = scandir(".", &namelist, 0, alphasort); if (n < 0) perror("scandir"); else { while (n--) { printf("%s\n", namelist[n]->d_name); free(namelist[n]); } free(namelist); } } --------------------------------------------------2
scandir()函数2011-08-24 14:20
#include <dirent.h> int scandir( const char *dir, struct dirent **namelist, int (*filter) (const void *b), int ( * compare )( const struct dirent **, const struct dirent ** ) ); int alphasort(const void *a, const void *b); int versionsort(const void *a, const void *b); 函数scandir扫描dir目录下以及dir子目录下满足filter过滤模式的文件,返回的结果是compare函数经过排序的,并保存在namelist中。注意namelist是通过malloc动态分配内存的,所以在使用时要注意释放内存。alphasort和versionsort是使用到的两种排序的函数。 当函数成功执行时返回找到匹配模式文件的个数,如果失败将返回-1。 eg: #include <dirent.h> int main() { struct dirent **namelist; int n; n = scandir(".", &namelist, 0, alphasort); if (n < 0) { perror("not found\n"); } else { while(n--) { printf("%s\n", namelist[n]->d_name); free(namelist[n]); } free(namelist); } } ---------------------------------------------------------------------------------3
dirent 结构体 介绍:
LINUX系统下的一个头文件,在这个目录下/usr/include
为了获取某文件夹目录内容,所使用的结构体。
引用头文件#include<dirent.h>
结构体说明
struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t 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 [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}
相关函数
opendir(),readdir(),closedir();
使用实例
#include <stdio.h> #include <errno.h>
#include <string.h> #include <sys/types.h>
#include <dirent.h>
#ifndef DT_DIR
#error "DT_DIR not defined, maybe d_type not a mumber of struct dirent!"
#endif
int
main(int argc, char*argv[])
{
staticchar dot[] =".", dotdot[] ="..";
constchar*name;
DIR *dirp;
struct dirent *dp;
if (argc ==2)
name = argv[1];
else
name = dot;
dirp = opendir(name);
if (dirp == NULL) {
(void)fprintf(stderr, "%s: opendir(): %s: %s\n",
argv[0], name, strerror(errno));
exit(errno);
}
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_type == DT_DIR)
if ( strcmp(dp->d_name, dot)
&& strcmp(dp->d_name, dotdot) )
(void)printf("%s/\n", dp->d_name);
}
(void)closedir(dirp);
return (0);
}
|