I am a C beginner and I am a bit confused about how to do this. I am trying with readdir
and strcmp
functions but it throws me a lot of errors.
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dirp;
struct dirent *direntp;
dirp = opendir(argv[1]);
if (dirp == NULL)
{
printf("File could not be open\n");
return -1;
}
int i = 0;
while((direntp[i]=readdir(dirp)) != NULL)
{
if(strcmp(direntp[i], argv[2]) == 0)
{
printf("The file %d is in directory %s my friend!", argv[2], dirp);
}
i++;
}
closedir(dirp);
return 0;
}