免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1298 | 回复: 3
打印 上一主题 下一主题

递归打印文件名的。 代码,不对, [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-28 18:55 |只看该作者 |倒序浏览
1 #include <stdio.h>
      2 #include <dirent.h>
      3 #include <string.h>
      4 #include <sys/types.h>
      5 #include <unistd.h>
      6 #include <sys/stat.h>
      7
      8 int main( void )
      9 {
     10     //
     11     chdir("./dir");
     12     renameAll("dir");
     13     return 1;
     14 }
     15
     16 /*  */
     17 int renameAll( char *pathname )
     18 {
     19     // tell us type of the file
     20     int dir = is_dir( pathname );
     21     if( dir == -1 ) {
     22         printf( "Error: Fu*king. No such directory!" );
     23         return -1;
     24     }
     25
     26     // open dir
     27     chdir(pathname);
     28     DIR *dp = opendir( pathname );
     29     if(dp == NULL) {
     30         printf( "Error: Can't read dir!\n" );
     31         printf("pathname=%s\n", pathname);
     32         //exit(1);
     33         return -1;
     34     }
     35
     36     // read dir
     37     struct dirent *ptr;
     38     struct stat statbuf; // Record the info
     39     while ( (ptr=readdir(dp)) != NULL ) {
     40
     41         //printf("name=%s, type=%d\n", ptr->d_name, ptr->d_type);
     42
     43         lstat( ptr->d_name, &statbuf );
     44         if ( S_ISDIR(statbuf.st_mode ) && (ptr->d_name != ".") && (ptr->d_name != "..") ) {
     45             chdir( ptr->d_name );
     46             renameAll( ptr->d_name );
     47         } else {
     48             printf("F**king: name=%s, type=%d\n", ptr->d_name, ptr->d_type);
     49         }
     50
     51     }
     52     closedir( dp );
     53
     54     return -1;
     55 }
     56
     57
     58 int is_dir(const char *pathname)
     59 {
     60     struct stat buf;
     61     memset(&buf, 0, sizeof(buf));
     62     lstat(pathname, &buf);
     63     if( S_ISDIR(buf.st_mode) ) {
     64         return 1;
     65     }
     66     return 0;
     67 }



我是希望递归的打印出各个文件夹底下的文件名的。不打印文件夹名。

论坛徽章:
0
2 [报告]
发表于 2006-11-29 09:21 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2006-11-29 10:21 |只看该作者
实际并不仅仅是打印文件名字出来,
真实的愿望是修改文件名。  把所有以.a结尾的文件改成.b。:)

论坛徽章:
0
4 [报告]
发表于 2006-11-30 08:53 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP