2

I am trying to debug right now in C and am curious if it is alright to call opendir() repeatedly without having to first call closedir() because I am trying to run a loop to open sub-directories when the while-loop that calls readdir() encounters them. And I assume that closing the current directory would cause me to lose the ability to read it.

tshepang
  • 12,111
  • 21
  • 91
  • 136
KenjiOne
  • 149
  • 2
  • 3
  • 10
  • Are you assigning the result of opendir to the same local variable? If you post your code with actual vs. expected results it makes it easier to answer your question. – mlibby Nov 16 '09 at 15:49
  • Well, I tried to make it recursive ... I feel that my problem might be another issue dealing with threads ... – KenjiOne Nov 16 '09 at 17:31

3 Answers3

5

You can use opendir() repeatedly. Just keep your DIR *s and call closedir() when you're done with each.

Wernsey
  • 5,411
  • 22
  • 38
2

Yes, you can do multiple opendir() calls. Be wary of symbolic link loops which could cause you to get into an infinite loop and consume all available memory.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
1

You'll have to be careful not to leak the DIR* that you are collecting, but as long as you don't hit the file-descriptor limit you shouldn't have any problem with having multiple opendir() open at the same time.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137