opendir ,readdir 测试demo
#include<sys/types.h> #include <stdio.h> #include<dirent.h> #include<unistd.h> int main(int argc,char **argv) { DIR * dir; struct dirent * ptr; int i; if(argc==1) dir=opendir("./"); else dir=opendir(argv[1]); while((ptr=readdir(dir))!=NULL) { printf("d_name: %s\n",ptr->d_name);//需要更详细的信息你可以修改该句 } closedir(dir); return 0; }
读取当前目录并显示当前目录下的内容.
测试通过.
运行结果:
yuanwei@lol:~/test$ gcc -o readir readdir.c yuanwei@lol:~/test$ l readdir.c readir* yuanwei@lol:~/test$ ls readdir.c readir yuanwei@lol:~/test$ ./readir d_name: readdir.c d_name: . d_name: .. d_name: readir