免费注册 查看新帖 |

Chinaunix

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

[C] 实在查不出错误了,大侠帮忙 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-14 11:59 来自手机 |只看该作者 |倒序浏览
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <strings.h>
void list(DIR *d,char *p){
        DIR *dp=(DIR *)d;
        struct dirent *dir;
        char path[256];
        bzero(path,256);
        strcpy(path,p);
        char bufpath[1024];
        bzero(bufpath,1024);
        struct stat *st;
        while(dir=readdir(dp)){
                strcpy(bufpath,path);
                strcat(bufpath,dir->d_name);
                stat(bufpath,st);
                if(S_ISDIR(st->st_mode) && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0){
                        strcat(bufpath,"/");
                        dp=opendir(bufpath);
                        list(dp,bufpath);
                }else{
                        printf("%s\n",bufpath);
                }
        }
}
int main(int argc,char **argv){
        char bufpath[256];
        bzero(bufpath,256);
        if(argc!=2){
                printf("miss argument\n");
        }else{
                strcpy(bufpath,argv[1]);
                int len=strlen(argv[1]);
                if(bufpath[len-1]!='/'){
                        strcat(bufpath,"/");
                }
        }
        DIR *dp;
        dp=opendir(bufpath);
        if(dp==NULL){printf("%s is not a dir!\n");}
        list(dp,bufpath);

}

论坛徽章:
0
2 [报告]
发表于 2013-04-14 13:38 |只看该作者
没看出啥  只是看出了 最后的那个 printf 少个参数 bufpath.....

论坛徽章:
0
3 [报告]
发表于 2013-04-14 16:57 |只看该作者
忘了附上错误图片了  就是这么个情况  小菜实在查不出错误了 排错排了好长时间

论坛徽章:
0
4 [报告]
发表于 2013-04-14 16:59 |只看该作者
错误的图片在下面是内存的错误好像

论坛徽章:
0
5 [报告]
发表于 2013-04-14 19:22 |只看该作者
你确定你的代码能通过编译?如果你 gcc -o 目标文件 源文件 -g -Wall
会发现很多的问题:
if(dp==NULL){printf("%s is not a dir!\n");}
这本身就是个错误。另外还有一个不好习惯:while的条件中采用赋值号,虽然可以用,但是非常不建议这样做while(dir=readdir(dp))
问题出在 stat(bufpath,st);这个地方。st你给出的是一个指针,其实应该是:struct stat stenty;struct stat* st = & stenty, stat(bufpath, st).st指向的空间应该提前初始化。
另外建议规范代码的书写风格。二元操作符尽量在操作数和操作符之间加空格。另外 函数名、if和while等与{尽量放在两行。

论坛徽章:
0
6 [报告]
发表于 2013-04-14 21:53 |只看该作者
本帖最后由 anpufeng 于 2013-04-14 21:58 编辑
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <errno.h>
  9. void list(DIR *d, char *p){
  10.         DIR *dp=(DIR *)d;
  11.         struct dirent *dir;
  12.         char path[256];
  13.         bzero(path,256);
  14.         strcpy(path,p);
  15.         char bufpath[1024];
  16.         bzero(bufpath,1024);
  17.         struct stat st;
  18.         while((dir = readdir(dp)) != NULL){
  19.                 strcpy(bufpath,path);
  20.                 strcat(bufpath,dir->d_name);
  21.                 if (stat(bufpath, &st) == 0) {
  22.                         if(S_ISDIR(st.st_mode) && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0) {
  23.                                 strcat(bufpath,"/");
  24.                                 printf("test in st_mode");
  25.                                 printf("%s's size is %-4d bytes\n", bufpath, st.st_size);
  26.                                 printf("%s's t_blksize is %-4d bytes\n", bufpath, st.st_blksize);
  27.                                 printf("%s's blocks is %-4d blocks\n", bufpath, st.st_blocks);
  28.                                 dp=opendir(bufpath);
  29.                                 list(dp,bufpath);
  30.                         } else {
  31.                                 printf("%s\n",bufpath);
  32.                         }
  33.                 } else {
  34.                         printf("bufpath: %s, dir->d_name: %s errorno: %s\n", bufpath,dir->d_name, strerror(errno));
  35.                 }
  36.         }
  37. }
  38. int main(int argc,char **argv) {
  39.         char bufpath[256];
  40.         bzero(bufpath,256);
  41.         if(argc!=2) {
  42.                 printf("miss argument\n");
  43.         } else {
  44.                 strcpy(bufpath,argv[1]);
  45.                 int len=strlen(argv[1]);
  46.                 if(bufpath[len-1]!='/')  {
  47.                         strcat(bufpath,"/");
  48.                 }
  49.         }
  50.         DIR *dp;
  51.         dp=opendir(bufpath);
  52.         if(dp == NULL){
  53.                 printf("%s is not a dir!\n", bufpath);
  54.         }
  55.         list(dp,bufpath);
  56. }
复制代码
stat()执行的时候要判断是否正确, 不要直接用
另外就是楼上说的好的编程习惯

论坛徽章:
0
7 [报告]
发表于 2013-04-15 19:34 来自手机 |只看该作者
谢了,领教了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP