免费注册 查看新帖 |

Chinaunix

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

請幫助我,謝謝! [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:51:16
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-29 18:29 |只看该作者 |倒序浏览
我寫了個 linux 讀取目錄下所有文件打印出來,包括子目錄裏的文件
但是有問題,本人很菜,看了3個小時都沒看出錯誤.希望大家幫忙.指點一下!!萬分感謝!!

#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include<stdlib.h>
#include <unistd.h>
#include<sys/stat.h>

void find(DIR *tdp)
{
   DIR *tmpdp;
   struct dirent *dirp;
   while((dirp = readdir(tdp)) != NULL)
   {
      if ((tmpdp = opendir(dirp->d_name)) == NULL)
      {
         printf("%s\n",dirp->d_name);
      }
      else
     {
       find(tmpdp);
      }
   }

int main()
{
   DIR *dp;
   if((dp = opendir("/home/freedino/test")) == NULL)
   {
      printf("can't open \n");
      exit(2);
   }
   find(dp);
}

论坛徽章:
0
2 [报告]
发表于 2006-09-29 19:35 |只看该作者
看K&R的那本书,上面有一个完整的程序

论坛徽章:
0
3 [报告]
发表于 2006-09-29 19:57 |只看该作者


[ 本帖最后由 leyyer 于 2006-9-29 20:44 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-09-29 20:04 |只看该作者
man ftw

论坛徽章:
0
5 [报告]
发表于 2006-09-29 20:53 |只看该作者

  1. #include<sys/stat.h>
  2. #include<unistd.h>
  3. #include<ftw.h>
  4. #include<stdio.h>
  5. int fn(const char *file,const struct stat *sb,int flag)
  6. {
  7. if(flag==FTW_D)printf("%s\t\t<-directory\n",file);
  8. else if(flag==FTW_F)printf("%s\t\t<-file\n",file);
  9. return 0;
  10. }
  11. int main()
  12. {
  13. char *buf;
  14. ftw(getcwd(buf,255),fn,100);
  15. return 0;
  16. }
复制代码

有现成的就用现成的没的自己写

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:51:16
6 [报告]
发表于 2006-10-13 17:57 |只看该作者

謝謝大家

#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include<stdlib.h>
#include <unistd.h>
#include<sys/stat.h>
#include<string.h>

void find( char *path, char *src)
{
   DIR *tmpdp;
   struct dirent *dirp;
   char s[1000];

   sprintf( s, "%s/%s", path, src);
   if( (tmpdp=opendir( s)) == NULL ){
      return;
   }

   fprintf( stderr, "%s\n", s );
   while((dirp = readdir(tmpdp)) != NULL){
      if (strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0)
         continue;

      find( s, dirp->d_name );
   }
}


int main( int argc, void **argv )
{
   find( "/home/freedino/", "test");
   return 0;
}


謝謝你們的幫助,謝謝!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP