我在第二次調用時打開同一目錄時出現問題。 例如,我先打開folder1/folder2;那麼如果我調用我在folder1上使用的函數,它說它無法打開它。我雖然我會關閉一個路徑中的所有目錄,並試圖做到這一點,但沒有結果。 這是我的代碼第二次無法打開目錄
void scanDir(char *dir, int depth, char type, char *path, long gtsize, int attrib)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
char newPath[strlen(path)+strlen(dir)];
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"Cannot open directory %s\n because of e", dir);
exit(10);
return;
}
strcpy(newPath, path);
strcat(newPath, dir);
if (type!='f' && testAttrib(attrib, dir))
printf("%s\n", newPath);
strcat(newPath, "/");
chdir(dir);
while((entry = readdir(dp)) != NULL) {
stat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode) && testAttrib(attrib, entry->d_name)) {
if(!strcmp(".",entry->d_name) || !strcmp("..",entry->d_name))
continue; // ignore . and ..
if (depth>1 || depth<=-1)
scanDir(entry->d_name,depth-1,type,newPath,gtsize,attrib);
}
if(S_ISREG(statbuf.st_mode) && type!='d' && testAttrib(attrib, entry->d_name)) {
off_t sizeF = statbuf.st_size;
char filePath[100];
strcpy(filePath, newPath);
strcat(filePath, entry->d_name);
if(sizeF>=gtsize)
printf("%s \n", filePath);
}
}
chdir("..");
closedir(dp);
}
那麼,看看錯誤?例如'perror()'。 – Stargateur
它加入第一個if並打印「無法打開目錄」 –