当前位置: 技术问答>linux和unix
这段代码不能遍历所有目录,请大神们帮看看
来源: 互联网 发布时间:2016-12-26
本文导语: 这段代码不能遍历所有目录,请大神们帮看看 #include #include #include #include void dirls(DIR *dirp,int num); int main(int argc,char **argv) { ssize_t readnum; DIR *dirp; struct dirent *direntp; if(argcd_name); if(0!=strcmp(direntp...
这段代码不能遍历所有目录,请大神们帮看看
#include
#include
#include
#include
void dirls(DIR *dirp,int num);
int main(int argc,char **argv)
{
ssize_t readnum;
DIR *dirp;
struct dirent *direntp;
if(argcd_name);
if(0!=strcmp(direntp->d_name,".")&&0!=strcmp(direntp->d_name,"..")&&NULL!=(dir=opendir(direntp->d_name)))
{
fprintf(stdout,"n***********************%s*******************************n",direntp->d_name);
dirls(dir,num+1);
fprintf(stdout,"n#######################%s##################################n",direntp->d_name);
}
}
closedir(dirp);
}
#include
#include
#include
#include
void dirls(DIR *dirp,int num);
int main(int argc,char **argv)
{
ssize_t readnum;
DIR *dirp;
struct dirent *direntp;
if(argcd_name);
if(0!=strcmp(direntp->d_name,".")&&0!=strcmp(direntp->d_name,"..")&&NULL!=(dir=opendir(direntp->d_name)))
{
fprintf(stdout,"n***********************%s*******************************n",direntp->d_name);
dirls(dir,num+1);
fprintf(stdout,"n#######################%s##################################n",direntp->d_name);
}
}
closedir(dirp);
}
|
恰好写过一个,仅供参考。
/****************************
遍历文件 夹中所有的文件 及目录.
自己实现了chdir()函数
**************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct passwd *pwd;
struct group *grp;
void info(struct stat *m, char *name) // 输出文件的信息
{
char str2[3] = {'r','w','x'};
stat(name,m);
switch(((*m).st_mode & S_IFMT))
{
case S_IFREG : printf("-");break;
case S_IFDIR : printf("d");break;
case S_IFCHR: printf("c");break;
case S_IFBLK: printf("b");break;
case S_IFIFO: printf("p");break;
case S_IFLNK: printf("l");break;
case S_IFSOCK: printf("s");break;
}
int tmp = 00400;
int i;
for(i = 0; i >1;
}
printf(" %10ld", (long) (*m).st_ino);
printf(" %-6ld", (long) (*m).st_nlink);
if((pwd = getpwuid(m->st_uid)) != NULL)
printf(" %-8.8s",pwd->pw_name);
else
printf(" %-8.8s",(char *)m->st_uid);
if((grp = getgrgid(m->st_gid)) != NULL)
printf(" %-8.8s",grp->gr_name);
else
printf(" %-8.8s",(char *)m->st_gid);
printf(" %10lld" ,(long long) (*m).st_size);
printf(" %s",name);
printf("n");
}
void info_dir(DIR *dp,char *name)
{
struct stat mm;
struct dirent *ep;
int i = 0;
while(ep = readdir(dp)){
i = 0;
char buf[100];
strcpy(buf,name);
if(ep->d_name[0] != '.'){
while(buf[i] != '')
{
i++;
}
if(buf[i-1] != '/'){
buf[i] = '/'; //在给定的路径下加/再传到info函数
i++;
buf[i] = '';
}
strcat(buf,ep->d_name);
info(&mm,buf);
}
}
}
int main(int argc ,char **argv)
{
char buf[100];
struct stat m;
DIR *dp;
if(argc 1;
}
printf(" %10ld", (long) (*m).st_ino);
printf(" %-6ld", (long) (*m).st_nlink);
if((pwd = getpwuid(m->st_uid)) != NULL)
printf(" %-8.8s",pwd->pw_name);
else
printf(" %-8.8s",(char *)m->st_uid);
if((grp = getgrgid(m->st_gid)) != NULL)
printf(" %-8.8s",grp->gr_name);
else
printf(" %-8.8s",(char *)m->st_gid);
printf(" %10lld" ,(long long) (*m).st_size);
printf(" %s",name);
printf("n");
}
void info_dir(DIR *dp,char *name)
{
struct stat mm;
struct dirent *ep;
int i = 0;
while(ep = readdir(dp)){
i = 0;
char buf[100];
strcpy(buf,name);
if(ep->d_name[0] != '.'){
while(buf[i] != '')
{
i++;
}
if(buf[i-1] != '/'){
buf[i] = '/'; //在给定的路径下加/再传到info函数
i++;
buf[i] = '';
}
strcat(buf,ep->d_name);
info(&mm,buf);
}
}
}
int main(int argc ,char **argv)
{
char buf[100];
struct stat m;
DIR *dp;
if(argc