I cannot get this function to work, because for some reason opendir will not take buffer2 (declared as char buffer2[128]) as an argument properly. If I replace the variable with something like "." or "sample", it works perfectly. But doing it like this, I get a segmentation fault every time. Please help.
system("clear");
DIR *dirp;
struct dirent *dp;
printf("Enter directory name: ");
fgets(buffer2, 100, stdin);
if((dirp = opendir(buffer2)) == NULL)
printf("Could not open directory\n");
while((dp = readdir(dirp)) != NULL)
printf("%s\n", dp->d_name);
closedir(dirp);
printf("Hit enter to return to selection.");
getchar();