我试图遍历一个给出路径的目录,但下面的程序没有进入目录并且卡在无限循环中。这段代码有什么问题:
void func(char path[]);
int main(int argc, char *argv)
{
char buf[255];
getcwd(buf,255);
func(buf);
}
void func(char path[])
{
DIR *dirp;
struct stat states;
struct dirent *direntp;
dirp=opendir(path);
chdir(path);
while((direntp=readdir(dirp))!=NULL)
{
if(S_ISDIR(states.st_mode))//true than returns zero
{
printf("%s\n",direntp->d_name);
}
else
if(!(strcmp(direntp->d_name,".")||strcmp(direntp->d_name,"..")))// if true retrns zero
{
continue;
}
else
{
func(direntp->d_name);
chdir("..");
}
}
}