这篇文章为转载;
出处:http://keyknight.blog.163.com/blog/static/3663784020093923732865/
include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<dirent.h>
int main()
{
DIR *dp;
struct dirent *dirp;
char dirname[]="/rocrocket/career/programming";
if((dp=opendir(dirname))==NULL){
perror("opendir error");
exit(1);
}
while((dirp=readdir(dp))!=NULL){
if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
continue;
printf("%6d:%-19s %5s\n",dirp->d_ino,dirp->d_name,(dirp->d_type==DT_DIR)?("(DIR)"):(""));
}
return 0;
}
程序运行结果如下:
[rocrocket@rocrocket programming]$ ./a.out 744045:opendir.c 744023:malloc_1.c 744025:newline_1.c 744027:sscanf_conf.c 744026:socket_test (DIR)744040:straight_insert.c 744020:abc 744036:const_1.c