免费注册 查看新帖 |

Chinaunix

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

[C++] 新手求解关于迭代器类型转换 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-11-22 17:17 |显示全部楼层 |倒序浏览
问题出在
ListDIR(*it->c_str());
res=stat(*it->c_str(),&s);
这两句上面,看报错应该是类型的问题,一时也弄不明白该怎么弄,不转换char也不行 string在stat函数中也用不了。求大神解惑,越详细越好。
  1. using namespace std;
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <list>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. int ListDIR(char *dir)
  8. {
  9.         int files=0;
  10.         int dirs=0;
  11.         DIR *p=opendir(dir);
  12.         if(p==NULL){
  13.                 cout << "not directory or not exist." << endl;
  14.                 return -1;
  15.         }
  16.         dirent *pd=NULL;
  17.         list<string> ls;
  18.         while((pd=readdir(p))!=NULL){
  19.                 if(pd->d_name[0]=='.')
  20.                         continue;
  21.                 if(pd->d_name[1]=='..')
  22.                         continue;
  23.                 ls.push_back(pd->d_name);
  24.         }
  25.         list<string>::iterator it;
  26.         it=ls.begin();
  27.         int res;
  28.         struct stat s;
  29.         while(it!=ls.end()){
  30.                 res=stat(*it->c_str(),&s);
  31.                 if(S_ISDIR(s.st_mode)){
  32.                         dirs++;
  33.                         ListDIR(*it->c_str());
  34.                 }
  35.                 files++;
  36.                 it++;
  37.         }
  38.         cout << "files=" << files <<endl;
  39.         cout << "dirs=" << dirs << endl;
  40. }
  41. int main(int argc,char *argv[])
  42. {
  43.         char *dir=NULL;
  44.         if(argc==1)
  45.                 dir=".";
  46.         else
  47.                 dir=argv[1];
  48.         ListDIR(dir);
  49. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-11-23 15:38 |显示全部楼层
回复 2# heguangwu


    求解,新手刚刚开始学习,所以不太明白错在哪了 为什么错了。大神可否解惑?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP