c++ - Unlink Not Removing File -


trying understand why unlink isn't working (not removing file) in code down below. thing can imagine program thinks i'm still interacting file not unlinking since still in use. code meant copy of "rm"

void directorysearch(const char *dname) {         dir *dir;         struct dirent *ent;         if ((dir = opendir (dname)) != null)         {                 while ((ent = readdir (dir)) != null)                 {                          if ( ent->d_type!=dt_dir)                         {                                 //where crazy happens                                 printf ("%s\n", ent->d_name);                                 char path[path_max];                                 const char * d_name = ent->d_name;                                 unlink(path);                         }                         if ( ent->d_type==dt_dir && strcmp(ent->d_name, ".")!= 0 && strcmp(ent->d_name, "..") != 0)                         {                                 int path_length;                                 char path[path_max];                                 const char * d_name = ent->d_name;                                  path_length = snprintf (path, path_max, "%s/%s", dname, d_name);                                  directorysearch(path);                         }                 }                 closedir (dir);         }         else         {                 cout << "error "<< dname<< endl;         } } 

edited unlink instead of remove, although both don't work...

you have declared path variable, not copied variable. that's problem. also, matter of course should examine return value unlink, , if less zero, examine errno determine exact nature of error. (typically permissions on no such file.)


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -