linux C语言实现递归删除文件文件夹功能
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 faile
·
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;
}
{
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;
}
更多推荐
已为社区贡献1条内容
所有评论(0)