c++读取文件的一些常规操作代码

  1. 获取文件夹下的所有文件名字
string image_dir = "/work/ai_lab/miner/os/Ultralight-SimplePose/ncnnsample/test_image/02/images";
vector<string> files;
void GetFileNames(string path,vector<string>& filenames)
{
    DIR *pDir;
    struct dirent* ptr;
    if(!(pDir = opendir(path.c_str())))
        return;
    while((ptr = readdir(pDir))!=0) {
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0)
            filenames.push_back(path + "/" + ptr->d_name);
    }
    closedir(pDir);
}
  1. 从路径中获取图片名:
std::string::size_type iPos = files[i].find_last_of('/') + 1;
std::string ImgName = files[i].substr(iPos, files[i].length() - iPos);
  1. 字符串拼接,直接两个相加即可
cv::imwrite(res_dir+ImgName, rgb);

你可能感兴趣的:(c++,开发语言)