I'm just starting with C programming, and i'm trying to read and display files in a directory (just like the ls command).
Here's a part of my code where I get a segfault, and I have no clue why:
void display_dir(char *dir)
{
DIR *strm;
struct dirent *direct;
if((strm = opendir(dir) == NULL))
{
printf("ERROR: Couldn't open directory.\n");
exit(1);
}
while ((direct = readdir(strm)) != NULL)
display_elems(direct);
closedir(strm);
}
After some tests, it appears that the program segfault when it reaches:
while ((direct = readdir(strm)) != NULL)
I've done some researches, but I didn't find anything that could help me.
if((strm = opendir(dir) == NULL))
The parentheses are nested wrong. It should be:
if((strm = opendir(dir)) == NULL)