tags:

views:

42

answers:

3

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;
    }
+3  A: 

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
Please, don't use `=` in if's. Even with the extra parenthesis, that's just bad code.
Helltone
@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
Thanks Alok, I got the answer
Sachin Chourasiya
'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.
Helltone
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
@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 Chourasiya
@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
Finally, I don't know what you mean by "= is not valid for comparison". `if (a = 1)` is a perfectly valid C statement.
Alok
+1  A: 

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
Thanks Douglas, I got the solution , My code is working properly now.
Sachin Chourasiya
A: 

Hi, 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

Helltone