浏览一个目录

来源:互联网 发布:下载2345软件大全 编辑:程序博客网 时间:2024/06/12 10:09
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stddef.h>
int main()
{
DIR *dp;
struct dirent *entry;
struct stat st;
char *dirp = "/home/rong";
if((dp = opendir(dirp)) == NULL)
{
perror("open /home:");
return -1;
}
chdir(dirp);
perror("/home");
while(entry = readdir(dp))
{
if(stat(entry->d_name,&st)!=-1)
{ printf("%s\t",entry->d_name);
if(st.st_mode&S_IFMT==S_IFDIR)
printf("dir\t");
else if(st.st_mode&S_IFMT==S_IFBLK)
printf("blk dievce\t");
else if(st.st_mode&S_IFMT==S_IFCHR)
printf("char dievce\t");
else if(st.st_mode&S_IFMT==S_IFREG)
printf("ord file\t");
//else 
//printf("unknown file\n");


}
}
close(dp);
return 0;
}
0 0