免费注册 查看新帖 |

Chinaunix

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

[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. }
复制代码

论坛徽章:
15
2015七夕节徽章
日期:2015-08-21 11:06:172017金鸡报晓
日期:2017-01-10 15:19:56极客徽章
日期:2016-12-07 14:07:30shanzhi
日期:2016-06-17 17:59:3115-16赛季CBA联赛之四川
日期:2016-04-13 14:36:562016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-01-28 06:20:0015-16赛季CBA联赛之新疆
日期:2016-01-25 14:01:34IT运维版块每周发帖之星
日期:2016-01-07 23:04:26数据库技术版块每日发帖之星
日期:2016-01-03 06:20:00数据库技术版块每日发帖之星
日期:2015-12-01 06:20:00IT运维版块每日发帖之星
日期:2015-11-10 06:20:00
2 [报告]
发表于 2015-11-22 19:59 |只看该作者
本帖最后由 heguangwu 于 2015-11-22 20:01 编辑

你这代码写的我也是醉了
随便搞了一下,主要是消除一些明显的错误和警告
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. #include <list>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <iostream>
  7. #include <string>
  8. using namespace std;
  9. int ListDIR(const char *dir)
  10. {
  11.         int files=0;
  12.         int dirs=0;
  13.         DIR *p=opendir(dir);
  14.         if(p==NULL){
  15.                 cout << "not directory or not exist." << endl;
  16.                 return -1;
  17.         }
  18.         dirent *pd=NULL;
  19.         list<string> ls;
  20.         while((pd=readdir(p))!=NULL){
  21.                 if(pd->d_name[0]=='.')
  22.                         continue;
  23.                 if(pd->d_name[0]=='.' && pd->d_name[1]=='.')
  24.                         continue;
  25.                 ls.push_back(pd->d_name);
  26.         }
  27.         list<string>::iterator it;
  28.         it=ls.begin();
  29.         int res;
  30.         struct stat s;
  31.         while(it!=ls.end()){
  32.                 res=stat((*it).c_str(),&s);
  33.                 if(S_ISDIR(s.st_mode)){
  34.                         dirs++;
  35.                         ListDIR(it->c_str());
  36.                 }
  37.                 files++;
  38.                 it++;
  39.         }
  40.         cout << "files=" << files <<endl;
  41.         cout << "dirs=" << dirs << endl;
  42. }
  43. int main(int argc,char *argv[])
  44. {
  45.         const char *dir=(argc == 1 ? "." : argv[1]);
  46.         int r=ListDIR(dir);
  47.         return r;
  48. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2015-11-23 15:38 |只看该作者
回复 2# heguangwu


    求解,新手刚刚开始学习,所以不太明白错在哪了 为什么错了。大神可否解惑?

论坛徽章:
15
2015七夕节徽章
日期:2015-08-21 11:06:172017金鸡报晓
日期:2017-01-10 15:19:56极客徽章
日期:2016-12-07 14:07:30shanzhi
日期:2016-06-17 17:59:3115-16赛季CBA联赛之四川
日期:2016-04-13 14:36:562016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-01-28 06:20:0015-16赛季CBA联赛之新疆
日期:2016-01-25 14:01:34IT运维版块每周发帖之星
日期:2016-01-07 23:04:26数据库技术版块每日发帖之星
日期:2016-01-03 06:20:00数据库技术版块每日发帖之星
日期:2015-12-01 06:20:00IT运维版块每日发帖之星
日期:2015-11-10 06:20:00
4 [报告]
发表于 2015-11-23 21:21 |只看该作者
回复 3# CrossGrave
难道你没有发现我特意写了两种不同的写法:
res=stat((*it).c_str(),&s);
ListDIR(it->c_str());

所谓迭代器其实可以理解成一个指向数据指针,然后你再查查C++运算符优先级就明白了


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP