문제

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top