我正在搜索当前目录中的目录,如果我跨越一个目录,我再次进入并搜索目录和文件,但我找不到问题。我总是得到相同的结果和一些特定的目录。在代码中,我在数组中获取目录名称并打印它们。结果如下所示;
direc Num (except parent and curr) : 6
/home/sabri/Desktop/Test/Untitled Folder 3
/home/sabri/Desktop/Test/Untitled Folder 3
/home/sabri/Desktop/Test/Untitled Folder 3
/home/sabri/Desktop/Test/Untitled Folder 3
/home/sabri/Desktop/Test
/home/sabri/Desktop/Test/Untitled Folder 4
/home/sabri/Desktop/Test/Untitled Folder 4
/home/sabri/Desktop/Test/Untitled Folder 4
但我的目录是;
Untitled Folder 1
Untitled Folder 2
Untitled Folder 3
Untitled Folder 4
Untitled Folder 5
int listFilesIndir(char *currDir)
{
struct dirent *direntp;
char newDir[20][250];
DIR *dirp;
int x ,y =0,i=0 ;
if ((dirp = opendir(currDir)) == NULL)
{
perror ("Failed to open directory");
return 1;
}
while ((direntp = readdir(dirp)) != NULL)
{
printf("%s\n", direntp->d_name);
if(direntp->d_type == DT_DIR)
{
y++;
chdir(direntp->d_name);
getcwd(newDir[i],250);
listNewDir(direntp->d_name);
i++;
}
}
printf("direc Num (except parent and current) : %d\n",y-2 );
for ( i = 0; i < 10; ++i)
{
printf("%s\n", newDir[i]);
}
while ((closedir(dirp) == -1) && (errno == EINTR)) ;
return 0;
}
int listNewDir(char *currDir)
{
struct dirent *direntp;
DIR *dirp;
if ((dirp = opendir(currDir)) == NULL)
{
perror ("Failed to open directory");
return 1;
}
while ((direntp = readdir(dirp)) != NULL)
printf("%s\n", direntp->d_name);
while ((closedir(dirp) == -1) && (errno == EINTR)) ;
return 0;
}