我正在开发一个 C 项目,我需要在其中获取目录中的文件列表。我正在使用 dirent.h,但在让它工作时遇到了一些问题,我正在 Linux 下构建程序。
当我尝试构建程序时,出现以下错误
myClass:error: âDIRâ undeclared (first use in this function)
myClass:408: error: (Each undeclared identifier is reported only once
myClass:408: error: for each function it appears in.)
myClass:408: error: âdirâ undeclared (first use in this function)
myClass:410: warning: implicit declaration of function âopendirâ
myClass:413: warning: implicit declaration of function âreaddirâ
myClass:413: warning: assignment makes pointer from integer without a cast
myClass:415: error: dereferencing pointer to incomplete type
myClass:417: warning: implicit declaration of function âclosedirâ
下面是我正在使用的代码
int logMaintenance(void *arg)
{
DIR *dir;
struct dirent *ent;
dir = opendir(directory);
if (dir != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
printf("%s\n", ent->d_name);
}
closedir(dir);
}
else
{
printf("Failed to read directory %i", EXIT_FAILURE);
}
return 0;
}
我不明白这些错误是什么意思,特别是当它说 DIR 在我包含 Liunux 的 dirent.h 头文件时未声明时。
谢谢你的帮助。