linux c++删除文件夹

在C++编程中有时需要删除一些文件或者文件夹。

头文件

#ifndef _FILEMANGEER_H
#define _FILEMANGEER_H

#include <iostream>
#include <string.h>
#include <fstream>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
using namespace std;
int removeFileAndDirectory(const std::string& path);
int copyFileAndDirectory(const std::string& old_path, const std::string& new_path);
int copyFile(const std::string& old_name, const std::string& new_name);
static bool isDirectory(const std::string& filefodler);
static bool isFile(const std::string& filename);
#endif

函数

#include "fileManage.h"

/* Check whether the path identified by 'filename' is a file */
static bool isFile(const std::string& filename) {
    struct stat   buffer;
    return (stat (filename.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
}

/* Check whether the path identified by 'filefodler' is a fodler */
static bool isDirectory(const std::string& filefodler) {
    struct stat   buffer;
    return (stat (filefodler.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
}

/* Copy the file named by old_name to new_name */
int copyFile(const std::string& old_name, const std::string& new_name)
{
    std::ifstream ifs(old_name, std::ifstream::binary);
    std::ofstream ofs(new_name, std::ifstream::binary| std::ifstream::trunc);
    if(ifs.good() == false)
    {
        return -1;
    }

    ofs << ifs.rdbuf();

    ifs.close();
    ofs.close();
    return 0;
}

/* Copy all files and sub directories in directory to new path */
int copyFileAndDirectory(const std::string& old_path, const std::string& new_path)
{
    int result = 0;
    DIR * p_dir;  
    struct dirent * p_dirent; 

    if(isDirectory(old_path))
    {
        if(access(new_path.c_str(),0) == -1)
        {
            if(mkdir(new_path.c_str(), 0755) < 0)
            {
                std::cout<< "[copyFileAndDirectory] mkdir failed , the errno = " << strerror(errno) << std::endl;
                return -1;
            }
        }

        if((p_dir = opendir(old_path.c_str())) == NULL )
        {  
            std::cout << "Usage:cp -r <src_dir> <dat_dir> error: " << strerror(errno) << std::endl;
            return -1;  
        }  

        while((p_dirent = readdir(p_dir)) != NULL)
        {
            std::string file_name = old_path + "/" + p_dirent->d_name;
            std::string new_file_name = new_path + "/" + p_dirent->d_name;
            // It is a directory
            if(isDirectory(file_name) && (0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, "..")))
            {
                result = copyFileAndDirectory(file_name,new_file_name);
                if(result < 0)
                {
                    return result;
                }
            }
            else if((0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, "..")))
            {
                result = copyFile(file_name, new_file_name);
                if(result < 0)
                {
                    return result;
                }
            }
        }
        closedir(p_dir);
    }
    return result;
}

/* Remove all files and sub directories in directory */
int removeFileAndDirectory(const std::string& path)
{
    int result = 0;
    DIR * p_dir;
    struct dirent * p_dirent;
    if(isDirectory(path))
    {
        if((p_dir = opendir(path.c_str())) == NULL )  
        {  
            std::cout << "Opendir error: " << strerror(errno) << std::endl;
            return -1;  
        }  

        while((p_dirent = readdir(p_dir)) != NULL)
        {
            std::string file_name = path + "/" + p_dirent->d_name;
            /* It is a directory */
            if(isDirectory(file_name) && (0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, "..")))
            {
                result = removeFileAndDirectory(file_name);
                if(result < 0)
                {
                    return result;
                }
            }
            /* It is a file */
            else if((0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, "..")))
            {
                result = remove(file_name.c_str());
                if(result < 0)
                {
                    return result;
                }
            }
            else
            {
                /* do nothing */
            }
        }
        closedir(p_dir);
        result = rmdir(path.c_str());
    }
    else if(isFile(path))
    {
        result = remove(path.c_str());
    }
    else
    {
        /* do nothing */
    }
    return result;
}

测试程序

#include "fileManage.h"

int main()
{
    //rm_dir("src");
    string des="src";
    removeFileAndDirectory(des);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值