讀取指定目錄下的所有檔案(windows 和 linux 版)

來源:互聯網
上載者:User

標籤:app   group   led   目錄   迭代   turn   char   部分   first   

 

筆者這裡用到了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;}

 

讀取指定目錄下的所有檔案(windows 和 linux 版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.