2
我想通过使用stat()
列出包含在目录UROP中的所有文件。但是,该目录不仅包含文件,还包含我想要搜索的文件夹。因此,我正在使用递归来访问要列出其文件的文件夹。无法区分文件夹
但是,我的循环中的if
条件无法区分文件和目录,并且所有文件都显示为目录;结果是无限递归代码如下。先谢谢你!
using namespace std;
bool analysis(const char dirn[],ofstream& outfile)
{
cout<<"New analysis;"<<endl;
struct stat s;
struct dirent *drnt = NULL;
DIR *dir=NULL;
dir=opendir(dirn);
while(drnt = readdir(dir)){
stat(drnt->d_name,&s);
if(s.st_mode&S_IFDIR){
if(analysis(drnt->d_name,outfile))
{
cout<<"Entered directory;"<<endl;
}
}
if(s.st_mode&S_IFREG){
cout<<"entered condition;"<<endl;
cout<<drnt->d_name<<endl;
}
}
return 1;
}
在S_ISDIR宏出现之前,通常使用'if((s.st_mode&S_IFMT)== S_IFDIR)...'。 – 2013-05-01 23:16:17
是的,这也会起作用。 – Scott 2013-05-01 23:19:58