Reputation: 57
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.
Upvotes: 0
Views: 3501
Reputation: 11921
first check given path really exist or not.
dirp = opendir("/home/cs429/Desktop/test");
man pages says "The opendir() functions return a pointer to the direc‐tory stream. On error, NULL is returned"
here you compared dirp
with NULL
thats correct but you putted semi-colon after if
statement, thats called dummy if
i.e whether condition is true or not, immediate statements will executes always. So remove the ;
after if
if(dirp == NULL)
{
//some code
}
Upvotes: 0
Reputation: 34585
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;
}
Upvotes: 1