- 论坛徽章:
- 0
|
本帖最后由 jiayanfu 于 2011-08-16 17:42 编辑
采用绝对路径,代码修改结果{:2_168:}
- #include<stdio.h>
- #include<unistd.h>
- #include<string.h>
- #include<dirent.h>
- void dirls(DIR *dirp,int num,char *pPath); //修改点1:增加路径参数
- int main(int argc,char **argv)
- {
- ssize_t readnum;
- DIR *dirp;
- struct dirent *direntp;
- if(argc<2)
- return 0;
- dirp=opendir(argv[1]);
- dirls(dirp,1,argv[1]);
- return 0;
- }
- void dirls(DIR *dirp,int num,char *pPath)
- {
- struct dirent *direntp;
- while(NULL != (direntp=readdir(dirp)))
- {
- DIR *dir;
- fprintf(stdout,"%d %s \n",num,direntp->d_name);
- //修改点2:增加路径参数
- char chTempDir[1024];
- memset(chTempDir,0,sizeof(chTempDir));
- sprintf(chTempDir,"%s//",pPath);
- strcat(chTempDir,direntp->d_name);
- if(0!=strcmp(direntp->d_name,".")&&0!=strcmp(direntp->d_name,"..")&&NULL!=(dir=opendir(chTempDir)))
- {
- fprintf(stdout,"\n***********************%s*******************************\n",direntp->d_name);
- dirls(dir,num+1,chTempDir);
- fprintf(stdout,"\n#######################%s##################################\n",direntp->d_name);
- }
- }
- closedir(dirp);
- }
复制代码 |
|