0

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;
    }
Sachin
  • 20,805
  • 32
  • 86
  • 99

3 Answers3

4

closedir() takes a DIR *, not a char *. Wishing closedir() did that is not going to work. Try:

#include <sys/types.h>
#include <dirent.h>

DIR *dir;
if ((dir = opendir(MyDir)) != NULL)
    closedir(dir);

Also, it seems you added a cast in (DIR *) MyDir to suppress a compiler warning. When a compiler gives you a warning, you should find out why it is doing so. Suppressing the warning is hardly the right thing to do.

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
  • Please, don't use `=` in if's. Even with the extra parenthesis, that's just bad code. – Giovanni Funchal Jan 19 '10 at 11:44
  • @Helltone: why is it bad code? Care to explain? The extra parentheses are needed in this case. (I could have written `if (dir = opendir(MyDir))` if I was going to write bad code [by my standard, some people might find it perfectly okay].) – Alok Singhal Jan 19 '10 at 11:51
  • 'if' is control flow. An assignment in an 'if' is considered harmful. As a general rule, never put assignment in any control structure. By the way, your 'bad code example' which 'some people find ok' will produce a warning in recent versions of gcc. – Giovanni Funchal Jan 19 '10 at 12:31
  • 1
    Yes, I know what `if` is. Do you oppose `while ((c = getchar()) != EOF)` as well? What you're saying is a matter of personal style/taste. – Alok Singhal Jan 19 '10 at 12:33
  • @Alok, I agreed with Helton. In the while condition you have stated, you are just getting the result of getchar() to c . This is perfectly fine, but the = is not valid for comparision. – Sachin Jan 20 '10 at 06:46
  • @Sachin: I *recognize* that other people might consider my style to be poor practice, but I am fairly sure that a significant fraction of people don't. For some construct to be accused of "bad code", most of the people in the programming community must think so. Also, the argument by Helltone was, "'if' is control flow". My response to that uses a similar construct in a `while` control flow, so either it must be "bad code" too (maybe Helltone thinks so), or "'if' is control-flow" is not a valid argument. – Alok Singhal Jan 20 '10 at 06:51
  • Finally, I don't know what you mean by "= is not valid for comparison". `if (a = 1)` is a perfectly valid C statement. – Alok Singhal Jan 20 '10 at 06:52
2

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);
}
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
0

the typecast is incorrect. For reference:

opendir needs a directory name (char*) as parameter and returns a directory stream (DIR*):

DIR* opendir(const char* name)

closedir needs a directory stream (DIR*) as parameter and returns an int (0 on success):

int closedir(DIR* stream)

So your code should look like:

const char* dirname;
DIR* mydir;

mydir = opendir(dirname);
if(mydir != NULL) {
   closedir(mydir);
   exit(0);
}

See also: http://sunsson.iptime.org/susv3/functions/readdir.html

Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110