Retrieving a list of files in a directory

I am working on a C project where I need to get a list of files in a directory. I use dirent.h, but I have problems with its operation, I am building a program under Linux.

When I try to create a program, I get the following error:

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â

Below is the code I'm using

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;
}

I do not understand what these errors mean, especially when it is said that the DIR is not declared when I included the dirent.h header file for Liunux.

Thank you for your help.

+3
source share
2 answers

You must ensure that:

  • You #include <dirent.h>, not "dirent.h", so the system search path for headers is used to search for this file
  • dirent.h, - , .

, GCC gcc -E. , ( ) . .

Microsoft Visual Studio, :
Microsoft Visual Studio: opendir() readdir(), ?

+3

, , , ... 8 (2 ) ++ . , , , :

int main(int argc, char **argv) int main(int argc, char *argv[]), int logMaintenance(void *arg)

( dirent.h).

0

All Articles