• c++ 常用的遍历,删除,分割等等文件处理函数代码实现


    原文作者:aircraft

    原文链接:https://www.cnblogs.com/DOMLX/p/9622851.html

    删除文件目录函数:

    void myDeleteDirectory(CString directory_path)
    {
        CFileFind finder;
        CString path;
        path.Format(_T("%s/*.*"), directory_path);
        BOOL bWorking = finder.FindFile(path);
        while (bWorking)
        {
            bWorking = finder.FindNextFile();
            if (finder.IsDirectory() && !finder.IsDots())//处理文件夹
            {
                myDeleteDirectory(finder.GetFilePath()); //递归删除文件夹
                RemoveDirectory(finder.GetFilePath());
            }
            else//处理文件
            {
                DeleteFile(finder.GetFilePath());
            }
        }
        RemoveDirectory(directory_path);
    }

    创建文件目录函数:

    int my_mkdir(const string &s)
    {
        const char *fileName = s.data(), *tag;
        for (tag = fileName; *tag; tag++)
        {
            if (*tag == '\')
            {
                char buf[1000], path[1000];
                strcpy(buf, fileName);
                buf[strlen(fileName) - strlen(tag) + 1] = NULL;
                strcpy(path, buf);
                if (access(path, 6) == -1)
                {
                    mkdir(path);  //创建成功返回0 不成功返回-1
                }
            }
        }
    }

    分割目录字符串,比如只取文件名字不要文件格式(也是取得.之前的字符串或者.之后的字符串),当然也可以分割其他字符,看你传入的操作字符是 . 还是 / 之类咯:

    vector<string> split(const string &s, const string &seperator){
        vector<string> result;
        typedef string::size_type string_size;
        string_size i = 0;
    
        while (i != s.size()){
            //找到字符串中首个不等于分隔符的字母;
            int flag = 0;
            while (i != s.size() && flag == 0){
                flag = 1;
                for (string_size x = 0; x < seperator.size(); ++x)
                    if (s[i] == seperator[x]){
                        ++i;
                        flag = 0;
                        break;
                    }
            }
    
            //找到又一个分隔符,将两个分隔符之间的字符串取出;
            flag = 0;
            string_size j = i;
            while (j != s.size() && flag == 0){
                for (string_size x = 0; x < seperator.size(); ++x)
                    if (s[j] == seperator[x]){
                        flag = 1;
                        break;
                    }
                if (flag == 0)
                    ++j;
            }
            if (i != j){
                result.push_back(s.substr(i, j - i));
                i = j;
            }
        }
        return result;
    }

    将某个字符串中的某些字符替换函数:

    string&   replace_all(string&   str, const   string&   old_value, const   string&   new_value)
    {
        while (true)   {
            string::size_type   pos(0);
            if ((pos = str.find(old_value)) != string::npos)
                str.replace(pos, old_value.length(), new_value);
            else   break;
        }
        return   str;
    }

    c++遍历文件目录获取文件名字:

    //获取特定格式的文件名
    void getAllFiles(string path, vector<string>& files, string format)
    {
        intptr_t    hFile = 0;//文件句柄  64位下long 改为 intptr_t
        struct _finddata_t fileinfo;//文件信息 
        string p;
        if ((hFile = _findfirst(p.assign(path).append("\*" + format).c_str(), &fileinfo)) != -1) //文件存在
        {
            do
            {
                if ((fileinfo.attrib & _A_SUBDIR))//判断是否为文件夹
                {
                    if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)//文件夹名中不含"."和".."
                    {
                        files.push_back(p.assign(path).append("\").append(fileinfo.name)); //保存文件夹名
                        getAllFiles(p.assign(path).append("\").append(fileinfo.name), files, format); //递归遍历文件夹
                    }
                }
                else
                {
                    files.push_back(p.assign(path).append("\").append(fileinfo.name));//如果不是文件夹,储存文件名
                }
            } while (_findnext(hFile, &fileinfo) == 0);
            _findclose(hFile);
        }
    }

    调用方法:

    std::string m_filePath = "模板\";
        std::vector<std::string> m_files;
        std::string m_format = "";                 //查找文件的格式
        getAllFiles(m_filePath, m_files, m_format);
        int size = m_files.size();
        int c_size;
        for (int i = 0; i < size; i++)
        {
                m_file[i];
            }

    以后用到其他c++的文件处理函数在补上去吧,反正来日翻墙。嘿嘿。。。

    若有兴趣交流分享技术,可关注本人公众号,里面会不定期的分享各种编程教程,和共享源码,诸如研究分享关于c/c++,python,前端,后端,opencv,halcon,opengl,机器学习深度学习之类有关于基础编程,图像处理和机器视觉开发的知识

  • 相关阅读:
    Dockerfile 构建前端node应用并用shell脚本实现jenkins自动构建
    Nginx自定义404页面
    shell脚本逐个杀死k8s中某个应用的pod
    阿里云容器镜像加速器配置
    jenkins shell脚本自动化构建阿里云k8s上应用
    在容器服务kubernetes上配置https
    docker build 指定dockerfile
    centos 7 系统启动不了 出现报错dependency failed for /mnt , dependency failed for local file systems
    向Kubernetes集群删除Node
    SharePoint 2013 关于自定义显示列表表单的bug
  • 原文地址:https://www.cnblogs.com/DOMLX/p/9622851.html
Copyright © 2020-2023  润新知