C 看似随机的opendir()失败
因此,我编写了一个简短的C程序,它可以搜索我计算机上的文件以查找某个文件。我编写了一个简单的函数,它接受一个目录,打开它并查看:C 看似随机的opendir()失败,c,opendir,C,Opendir,因此,我编写了一个简短的C程序,它可以搜索我计算机上的文件以查找某个文件。我编写了一个简单的函数,它接受一个目录,打开它并查看: int exploreDIR (char stringDIR[], char search[]) { DIR* dir; struct dirent* ent; if ((dir = opendir(stringDIR)) == NULL) { printf("Error: could not open
int exploreDIR (char stringDIR[], char search[])
{
DIR* dir;
struct dirent* ent;
if ((dir = opendir(stringDIR)) == NULL)
{
printf("Error: could not open directory %s\n", stringDIR);
return 0;
}
while ((ent = readdir(dir)) != NULL)
{
if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
if (strlen(stringDIR) + 1 + strlen(ent->d_name) > 1024)
{
perror("\nError: File path is too long!\n");
continue;
}
char filePath[1024];
strcpy(filePath, stringDIR);
strcat(filePath, "/");
strcat(filePath, ent->d_name);
if (strcmp(ent->d_name, search) == 0)
{
printf(" Found it! It's at: %s\n", filePath);
return 1;
}
struct stat st;
if (lstat(filePath, &st) < 0)
{
perror("Error: lstat() failure");
continue;
}
if (st.st_mode & S_IFDIR)
{
DIR* tempdir;
if ((tempdir = opendir (filePath)))
{
exploreDIR(filePath, search);
}
}
}
closedir(dir);
return 0;
}
问题是,我不知道这些文件是什么导致opendir()失败的。我没有在任何程序中打开它们。它们只是我在桌面上创建的简单文件夹。有人知道问题出在哪里吗?您每次调用
opendir()
两次closedir()
。可能您的资源不足。chmod r+x/Users/Dan/Desktop/Box/Videos
然后重试。只需通过sudo
执行检查errno
会告诉您什么?您会为每个closedir()调用opendir()两次。也许你的资源已经用完了。@ZagorulkinDmitry:坏主意。对于不需要的任何内容,不要使用sudo
,或一般的root访问。在这种情况下,它也不会有任何帮助。啊,这就是问题所在!我真不敢相信我忘记了第二个opendir()。非常感谢。
Error: could not open directory /Users/Dan/Desktop/Box/Videos
Error: could not open directory /Users/Dan/Desktop/compilerHome