2007-08-23
unix环境下标准C实现的遍历某个文件夹下面所有文件
unix环境下标准C实现的遍历某个文件夹下面所有文件(转自CSDN)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
int testdir(char *path)
{
struct stat buf;
if(lstat(path,&buf)<0)
{
return 0;
}
if(S_ISDIR(buf.st_mode))
{
return 1; //directory
}
return 0;
}
int directory(char *path)
{
DIR *db;
char filename[128];
struct dirent *p;
db=opendir(path);
if(db==NULL)return 0;
memset(filename,0,128);
while ((p=readdir(db)))
{
if((strcmp(p->d_name,".")==0)||(strcmp(p->d_name,"..")==0))
continue;
else
{
sprintf(filename,"%s/%s",path,p->d_name);
if(testdir(filename))
{
directory(filename);
}
else {
printf("%s\n",filename);
}
}
memset(filename,0,64);
}
closedir(db);
return 0;
}
int main(int argc,char **argv)
{
char *path="./"; //要遍历的目录
if(access(path,F_OK)==0&&testdir(path))
{
printf("is directory\n");
directory(path);
}
else printf("%s not exist\n",path);
}
- 浏览: 67453 次
- 性别:


- 详细资料
搜索本博客
最近加入圈子
链接
最新评论
-
Hibernate之deleted obje ...
不错,我也碰到这个问题了。
-- by winwnx -
Java 接口大全、面向接口 ...
厉害!学习了。谢谢!
-- by 31212 -
ET Limit方式的分页查询
不能排序怎么办办呢
-- by zhang20084 -
eXtreme Table 的基本用法 ...
为什么 我把的CSS不能调用?把TEST里面的<STYLE>去掉后,调用e ...
-- by fredweng -
分页标签:pager-taglib使 ...
发现LZ在没有声明的情况下引用了我大部分的文字。连“咳”都用过来了 :)http ...
-- by 我想我是海






评论排行榜