icenfire
icenfire

Reputation: 3

while ... readdir causing segmentation fault

In main I call the following function with deletefolder():

void deletefolder(){

   struct dirent *next_file;
   DIR *folder;

   char filepath[256];

   folder = opendir("./game/wow");

   while((next_file = readdir(folder)) != NULL){ //this is causing the segmentation fault. I don't know why?


      sprintf(filepath, "%s/%s", "./game/wow", next_file->d_name);
      remove(filepath);
   }
}

I can't figure out why it is happening?

Upvotes: 0

Views: 1330

Answers (1)

R Sahu
R Sahu

Reputation: 206717

while((next_file = readdir(folder)) != NULL){ //this is causing the segmentation fault. i dont know why??

I suspect there was an error in opening the directory. Add some error checking code.

folder = opendir("./game/wow");
if ( folder == NULL )
{
   perror("Unable to open folder.");
   return;
}

while((next_file = readdir(folder)) != NULL){ ... 

Upvotes: 2

Related Questions