Search code examples
cnullopendir

C opendir always NULL


So I am working on a directory traversal, and I am unable to get opendir to work the way I want it to. It always fails to open the directory I send it, it gives some unknown error. I usually pass in argv[1], but I gave up and just started hard coding paths.

char *dest = NULL;
char *filePath = ".";
char *newPath = NULL;


DIR *dirp;
struct dirent *dp;
int errno;
dirp = opendir("/home/cs429/Desktop/test");

struct stat path_stat;



if(dirp == NULL);
{
    // Error Handling
    fprintf(stderr, "Error failed to open input directory -%s\n",strerror(errno) );
    return 1;
}

Does anyone see what I could be doing to get NULL? I also get this in gdb: ../sysdeps/posix/opendir.c: No such file or directory.


Solution

  • The line

    if(dirp == NULL);
    

    does nothing, and the next code block is always executed, regardless of the value of dirp.

    Please remove the ; so the code is

    if(dirp == NULL)
    {
        // Error Handling
        fprintf(stderr, "Error failed to open input directory -%s\n",strerror(errno) );
        return 1;
    }