您的位置:首页 > 运维架构 > Linux

linux C语言实现递归删除文件文件夹功能

2015-08-31 19:49 836 查看
static int remove_dir(const char *dirname)

{

DIR *dir;

struct dirent *entry;

char path[PATH_MAX];

dir = opendir(dirname);

if (dir == NULL) {

LOGE("opendir %s failed\n", dirname);

return -1;

}

while ((entry = readdir(dir)) != NULL) {

if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {

snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname, entry->d_name);

if (entry->d_type == DT_DIR) {

remove_dir(path);

} else {

// delete file

unlink(path);

}

}

}

closedir(dir);

// now we can delete the empty dir

rmdir(dirname);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: