I am accepting the path through command line input.
When I do
dir=opendir(args[1]);
it doesn’ t enter the loop…i.e dir==null…
How do I pass the command line input to dir pointer?
void main(int c,char **args)
{
DIR *dir;
struct dirent *dent;
char buffer[50];
strcpy(buffer, args[1]);
dir = opendir(buffer); //this part
if(dir!=NULL)
{
while((dent=readdir(dir))!=NULL)
printf(dent->d_name);
}
close(dir);
}
./a.out /root/TEST is used to run the program..
./a.out --> to execute the program
/root/TEST --> input by the user i.e valid path
You should really post your code(a), but here goes. Start with something like:
You need to check in your case that
args[1]is both set and refers to an actual directory. A sample run, withtmpis a subdirectory off my current directory but you can use any valid directory, gives me:testprog tmp
Note also that you have to pass a directory in, not a file. When I execute:
I get:
That’s because it’s a file rather than a directory (though, if you’re sneaky, you can attempt to use
diropen(dirname(argv[1]))if the initialdiropenfails).(a) This has now been rectified but, since this answer has been accepted, I’m going to assume it was the issue of whatever you were passing in.