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
Post a Comment