• C++实用记录


    一、工作中有时会用到的一些功能,记录下来

    //遍历文件夹下的文件,输入:文件夹路径,返回文件名的vector
    void
    get_image_names(const string &file_path, vector<string>&file_names) { intptr_t hFile = 0; //文件信息的结构体 _finddata_t fileInfo; //第一次查找 hFile = _findfirst(file_path.c_str(), &fileInfo); if (hFile != -1) { do { if ((fileInfo.attrib & _A_SUBDIR)) { continue; } else { file_names.push_back(fileInfo.name); //cout << fileInfo.name << endl; } } while (_findnext(hFile, &fileInfo) == 0); _findclose(hFile); } }

    //调用方式


    vector<string> filenames;
    string filepath = "";
    get_image_names(filepath + "*.jpg", filenames);
     

    void getFiles(string path, vector<string>& files)
    /*获取目录下的文件*/
    {
    //文件句柄
    intptr_t hFile = 0;
    //文件信息
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = _findfirst(p.assign(path).append("\*").c_str(), &fileinfo)) != -1)
    {
    do
    {
    //如果是目录,迭代之,如果不是目录那一步删除
    if ((fileinfo.attrib & _A_SUBDIR))
    {
    if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
    getFiles(p.assign(path).append("\").append(fileinfo.name), files);
    }
    else
    {
    files.push_back(p.assign(path).append("\").append(fileinfo.name));
    }
    } while (_findnext(hFile, &fileinfo) == 0);
    _findclose(hFile);
    }
    }

    //另一种比较方便的方法是,利用opencv中的glob
    string filepath = "";
    vector<String> filenames;
    glob(filepath, filenames,true);
    for (auto c : filenames) {
    std::cout << "正在处理:" << c << "..." << std::endl;
            int pos = c.find_last_of('\');
            string s(c.substr(pos + 1));
    }          
    //不过数据类型为cv::String
    所以要不是opencv直接用一般需要转类型,比如char*或者string类型
    USES_CONVERSION;
    cv::String b = filename;
    string c = b;
    const char *p = c.data();
    
      
    //根据指定pattern对字符串进行切分,类似python中的split
    vector<string> split(const string &str, const string &pattern)
    {
        /*基于find和substr函数实现字符串切割*/
        vector<string> res;
        if (str == "")
            return res;
        //在字符串末尾也加入分隔符,方便截取最后一段
        string strs = str + pattern;
        //pos为strs中pattern出现的第一个位置
        size_t pos = strs.find(pattern);
        //npos在 c++中表示一个常数,表示不存在的位置,npos 是这样定义的static const size_type npos = -1;
        //所以pos 也为size_t类型,不能定义为int
        while (pos != strs.npos)
        {
            string temp = strs.substr(0, pos);
            res.push_back(temp);
            //去掉已分割的字符串,在剩下的字符串中进行分割
            strs = strs.substr(pos + 1, strs.size());
            pos = strs.find(pattern);
        }
        return res;
    }

    //调用举例

    vector<string> res;
    res = split(txtfile, "\");

     
    //解决读取中文编码乱码的情况
    string UTF8ToGB(const char* str)
    {
        string result;
        WCHAR *strSrc;
        LPSTR szRes;
    
        //获得临时变量的大小
        int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
        strSrc = new WCHAR[i + 1];
        MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
    
        //获得临时变量的大小
        i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
        szRes = new CHAR[i + 1];
        WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
    
        result = szRes;
        delete[]strSrc;
        delete[]szRes;
    
        return result;
    }
    //将文件复制到指定文件夹
    void copy_file(int flag,string s,string name) {
        string source = "";
        string target = "";
        string target1 = "";
        string source_path = source + s+ "\"+name;
        LPCSTR source_path1 = source_path.c_str();
        if (flag == 1){
            string target_path = target + s + "\";
            //cout << target_path << endl;
            if (0 != _access(target_path.c_str(), 0))
            {
                _mkdir(target_path.c_str());   // 返回 0 表示创建成功,-1 表示失败
            }
            string target_path_name = target_path + name;
            LPCSTR target_path_name1 = target_path_name.c_str();
            //cout << target_path_name1 << endl;
            CopyFile(source_path1, target_path_name1, TRUE);//false代表覆盖,true不覆盖
        }
        else {
            string target_path1 = target1 + s + "\";
            //cout << target_path << endl;
            if (0 != _access(target_path1.c_str(), 0))
            {
                _mkdir(target_path1.c_str());   // 返回 0 表示创建成功,-1 表示失败
            }
            string target_path_name1 = target_path1 + name;
            LPCSTR target_path_name2 = target_path_name1.c_str();
            //cout << target_path_name1 << endl;
            CopyFile(source_path1, target_path_name2, TRUE);//false代表覆盖,true不覆盖
        }
    
    }
  • 相关阅读:
    css 样式合集
    java-TheadPoolExecutor
    ConcurrentHashMap
    java线程
    hadoop 笔记
    java中的锁
    关闭防火墙
    hadoop集群搭建
    ssh免密码登录
    pyrhon 技巧 解析pymysql查询结果,取mysql其中的某一列数据 zip解压
  • 原文地址:https://www.cnblogs.com/lzq116/p/13220232.html
Copyright © 2020-2023  润新知