cclosedir

closedir () causing core dumped


I have below lines in my code written in C on unix platform. Please let me know why I am getting core dumped in closedir() function. I could successfully opened the directory specified by path.

    if (opendir(MyDir) != NULL )
    {
    closedir((DIR *) MyDir);
        exit 0;
    }

Solution

  • MyDir must be a const char* to be the argument for opendir.

    You need the result from opendir to pass to closedir - you can't just cast the path!

    const char* MyDir = "/";
    DIR* directory = opendir(MyDir);
    if (directory != NULL)
    {
        closedir(directory);
        exit(0);
    }