废话不多说,以下代码中在Remove方法中传入文件路径即可.
//Recursively delete all the file in the directory.
int rm_dir(std::string dir_full_path) {
DIR* dirp = opendir(dir_full_path.c_str());
if(!dirp)
{
return -1;
}
struct dirent *dir;
struct stat st;
while((dir = readdir(dirp)) != NULL)
{
if(strcmp(dir->d_name,".") == 0
|| strcmp(dir->d_name,"..") == 0)
{
continue;
}
std::string sub_path = dir_full_path + '/' + dir->d_name;
if(lstat(sub_path.c_str(),&st) == -1)
{
//Log("rm_dir:lstat ",sub_path," error");
continue;
}
if(S_ISDIR(st.st_mode))
{
if(rm_dir(sub_path) == -1) // 如果是目录文件,递归删除
{
closedir(dirp);
return -1;
}