• c++文件操作


    文件操作:

    #include <iostream>
    #include <string.h>
    #include <fstream>
    #include <cstdio>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <dirent.h>
    
    // //LINUX下历遍目录 opendir -> readdir -> closedir
    // // 打开 ->  读取  -> 关闭
    
    /* 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()) < 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;
    }
    
    int main(void)
    {
        //参数可以是绝对路径或者相对路径
        std::string src = "./123";
        std::string des = "./1234";
        int result = copyFileAndDirectory(src, des);
        std::cout<< result << std::endl;
        result = removeFileAndDirectory(src);
        std::cout<< result << std::endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
  • 相关阅读:
    聊聊 Kubectl scale 命令的优秀实践
    Neo4j导入RDF文件
    CRM软件系统维护客户的主要方法
    java多线程应用场景
    空调原理与结构、制冷剂类型及相关先进技术
    Ctrl+Alt+L或Ctrl+Alt+M或Ctrl+Alt+S热键无法启用(华硕天选)与armoury creat冲突
    【无标题】点击更新进度条位置
    Unity VR 开发教程: Oculus 一体机开发 (一) 环境配置(基于 Oculus Integration v46)
    重学java基础----反射
    计算机组成原理考研笔记
  • 原文地址:https://blog.csdn.net/why18767183086/article/details/125619984