[C] 递归目录并用stat获取文件信息错误...真诚求救!!!!

yisildeai 2012-09-09 03:26:26
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <assert.h>

static void TraveDir (char *DirName)
{
DIR *pDir;
struct dirent *DirHand;
struct stat *StaBuf;
pDir = opendir(DirName);
char ChildDir[256];
strcpy(ChildDir,DirName);
assert(pDir != NULL);

while( (DirHand = readdir(pDir)) != NULL )
{
if(DirHand->d_type == 4)
{
if( strcmp(DirHand->d_name,".") == 0 || strcmp(DirHand->d_name,"..") == 0 )
{
continue;
}
sprintf(ChildDir,"%s/%s",DirName,DirHand->d_name);
TraveDir(ChildDir);
}
else if(DirHand->d_type == 8)
{
char FileName[256];
sprintf(FileName,"%s/%s",ChildDir,DirHand->d_name);
stat(FileName,StaBuf);
printf("%s %ld\n",FileName,StaBuf->st_atime);
}

}
closedir(pDir);
}


int main(void)
{
char *path = "/tmp";
TraveDir(path);

return 1;
}


/tmp/back/backupconf/10.3.3.13/log4j.properties 1346293061
/tmp/back/backupconf/10.3.3.13/ip.txt 1346293061
/tmp/back/backupconf/10.3.3.12/system.properties 1346293061
/tmp/back/backupconf/10.3.3.12/struts.properties 1346293061
/tmp/back/backupconf/10.3.3.12/jdbc.properties 1346293061
/tmp/back/backupconf/10.3.3.12/application.properties 1346293061
/tmp/back/backupconf/10.3.3.12/accounttype.properties 1346293061
/tmp/back/backupconf/10.3.3.12/badnames.properties 1346293061
/tmp/back/backupconf/10.3.3.12/messages_zh_CN.properties 1346293061
/tmp/back/backupconf/10.3.3.12/list.txt 1346293061
/tmp/back/backupconf/10.3.3.12/log4j.properties 1346293061
/tmp/back/backupconf/10.3.3.12/backupconf.sh 1346293061
段错误

//执行到一半的时候出现 段错误..... 不知道是什么问题....求解....谢谢!!!
...全文
123 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yisildeai 2012-09-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

#include <stdlib.h>
[/Quote]


啊!!!!!!!!!!! 哎....丢人丢大了....... 抱歉....

这位哥....可否加你qq?
qq120848369 2012-09-09
  • 打赏
  • 举报
回复
#include <stdlib.h>
qq120848369 2012-09-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

struct stat *StaBuf;

内存呢?


这个该如何申请内存呢...

StaBuf = (struct stat *)malloc(sizeof(struct stat)); //这样申请 报错...
[/Quote]

错什么?
yisildeai 2012-09-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

struct stat *StaBuf;

内存呢?
[/Quote]

traversefile.c:12: 警告:隐式声明与内建函数 ‘malloc’ 不兼容

11 struct stat *StaBuf;
12 StaBuf = (struct stat *)malloc(sizeof(struct stat));
yisildeai 2012-09-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

struct stat *StaBuf;

内存呢?
[/Quote]

这个该如何申请内存呢...

StaBuf = (struct stat *)malloc(sizeof(struct stat)); //这样申请 报错...
qq120848369 2012-09-09
  • 打赏
  • 举报
回复
struct stat *StaBuf;

内存呢?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧