BBS 水木清华站 - 软件开发 - CProgramming (C程序设计语言)版(进版画面 | 添加到收藏夹 | 我的百宝箱 )
CProgramming(C程序设计语言) 版
版主 诚征版主中
在线 16 人 文摘区 | 精华区 | 版内查询
令狐冲精华区搜索引擎 搜索范围 全站 CProgramming版
精华区文章阅读
发信人: ilylx (ilylx), 信区: CProgramming
标 题: Re: c有没有处理目录的函数
发信站: BBS 水木清华站 (Tue Jan 11 14:17:33 2005), 站内
已经搞定了,找到了个例子#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
int printdir(char *dir,int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp=opendir(dir))==NULL)
{
printf("can not open directory :%s/n",dir);
return 0;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
stat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
{
continue;
}
printf("%*s%s//n",depth," ",entry->d_name);
printdir(entry->d_name,depth+4);
} else
printf("%*s%s/n",depth," ",entry->d_name);
}
chdir("..");
closedir(dp);
return 0;
}
int main()
{
printf("Directory scan of H://QQDownload:/n");
printdir("H://QQDownload",5);
printf("done!");
getch();
return 0;
}
【 在 srbga (做一个好人) 的大作中提到: 】
: ft,linux也有库函数
: 不过直接打开ls的管道读倒也确实方便
--
※ 修改:·ilylx 于 Jan 11 14:21:20 修改本文·[FROM: 202.112.155.*]
※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.112.155.*]
[返回顶部] [刷新] [同主题模式] [普通模式] [版内查询]
[我的百宝箱] [返回首页] [上级目录] [根目录] [令狐冲精华区搜索] [返回顶部] [刷新] [返回]