读取指定目录下的所有文件(windows 和 linux 版)

 

笔者这里用到了OpenCV,如果不需要用OpenCV代码的话,可以将这部分代码去掉即可。

windows  vs2015环境代码如下:

#include <io.h> // 结构体struct _finddata_t需要用到
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

char * fileLoadPath = "E:\\ubshare\\Cars\\102051724100";
char * fileDstPath = "E:\\ubshare\\Cars\\testdir\\";

void getFiles(string path, vector<string>& files)
{
    //文件句柄  
    long   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);
    }
}

int main()
{
    vector<string> files;

    ////获取该路径下的所有文件  
    getFiles(fileLoadPath, files);

    int size = files.size();
    for (int i = 0; i < size; i++)
    {
        string name = files[i].c_str() + strlen(fileLoadPath) + 1; // 从路径名中取出文件名
        cout << "files[i].c_str() = " << files[i].c_str() << ", name = " << name << endl;
        name.replace(name.find(".jpg"), 4, ".bmp"); // 将文件名的jpg后缀改为bmp后缀

        Mat img = imread(files[i].c_str());
        imshow(files[i].c_str(), img);
        string str = fileDstPath;
        str += name;
        imwrite(str, img); // 将图片按指定格式存入指定路径
    }
    waitKey(0);
    return 0;
}

 

linux版代码如下:

#include <iostream>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include <dirent.h>

using namespace std;
using namespace cv;

string gFileLoadPath = "/mnt/hgfs/ubshare/Cars/102051724100/group2/";
string gFileDstPath = "/mnt/hgfs/ubshare/Cars/102051724100result/";

int main()
{
    DIR *dir = opendir(gFileLoadPath.c_str());
    if (dir == NULL)
    {
        cout << "opendir error" << endl;
        return -1;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL)
    {
        //if (entry->d_type == 4) continue; //It's dir
        cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
        string name = entry->d_name;
        string imgdir = gFileLoadPath + name;
        Mat img = imread(imgdir.c_str());
        imshow(entry->d_name, img);

        string resultdir = gFileDstPath + name;
        imwrite(resultdir.c_str(), img);
    }
    closedir(dir);
    waitKey(0);
    //system("pause");
    return 0;
}

 

转载于:https://www.cnblogs.com/YahStudio/p/7043637.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值