- 论坛徽章:
- 0
|
问题出在
ListDIR(*it->c_str());
res=stat(*it->c_str(),&s);
这两句上面,看报错应该是类型的问题,一时也弄不明白该怎么弄,不转换char也不行 string在stat函数中也用不了。求大神解惑,越详细越好。- using namespace std;
- #include <sys/types.h>
- #include <dirent.h>
- #include <list>
- #include <sys/stat.h>
- #include <unistd.h>
- int ListDIR(char *dir)
- {
- int files=0;
- int dirs=0;
- DIR *p=opendir(dir);
- if(p==NULL){
- cout << "not directory or not exist." << endl;
- return -1;
- }
- dirent *pd=NULL;
- list<string> ls;
- while((pd=readdir(p))!=NULL){
- if(pd->d_name[0]=='.')
- continue;
- if(pd->d_name[1]=='..')
- continue;
- ls.push_back(pd->d_name);
- }
- list<string>::iterator it;
- it=ls.begin();
- int res;
- struct stat s;
- while(it!=ls.end()){
- res=stat(*it->c_str(),&s);
- if(S_ISDIR(s.st_mode)){
- dirs++;
- ListDIR(*it->c_str());
- }
- files++;
- it++;
- }
- cout << "files=" << files <<endl;
- cout << "dirs=" << dirs << endl;
- }
- int main(int argc,char *argv[])
- {
- char *dir=NULL;
- if(argc==1)
- dir=".";
- else
- dir=argv[1];
- ListDIR(dir);
- }
复制代码 |
|