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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值