Read all files in the specified directory (Windows and Linux editions)

Source: Internet
Author: User
Tags strcmp

I use the OpenCV here, if you do not need to use the OPENCV code, you can remove this part of the code.

The Windows VS2015 Environment code is as follows:

#include <io.h>//struct struct _finddata_t need to be used#include <opencv2/opencv.hpp>using namespaceCV;using namespacestd;Char* Fileloadpath ="e:\\ubshare\\cars\\102051724100";Char* Filedstpath ="e:\\ubshare\\cars\\testdir\\";voidGetFiles (stringPath, vector<string>&files) {    //file handle    Longhfile =0; //File Information    struct_finddata_t FileInfo; stringp; if(hfile = _findfirst (p.assign (Path). Append ("\\*"). C_str (), &fileinfo))! =-1)    {         Do        {            //in the case of a directory, the iteration//if not, join the list            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); }}intmain () {vector<string>files; ////Get all files under this pathgetFiles (Fileloadpath, files); intSize =files.size ();  for(inti =0; i < size; i++)    {        stringName = FILES[I].C_STR () + strlen (Fileloadpath) +1;//remove a file name from the pathnamecout <<"files[i].c_str () ="<< files[i].c_str () <<", name ="<< name <<Endl; Name.replace (Name.find (". jpg"),4,". bmp");//change the jpg suffix of the file name to the BMP suffixMat img=Imread (Files[i].c_str ());        Imshow (FILES[I].C_STR (), IMG); stringstr =Filedstpath; STR+=name; Imwrite (str, IMG); //save a picture to a specified path in the specified format} waitkey (0); return 0;}

The Linux code is as follows:

#include <iostream>#include<memory.h>#include<stdio.h>#include<stdlib.h>#include<opencv2/opencv.hpp>#include<dirent.h>using namespacestd;using namespaceCV;stringGfileloadpath ="/mnt/hgfs/ubshare/cars/102051724100/group2/";stringGfiledstpath ="/mnt/hgfs/ubshare/cars/102051724100result/";intMain () {DIR*dir =Opendir (Gfileloadpath.c_str ()); if(dir = =NULL) {cout<<"Opendir Error"<<Endl; return-1; }    structDirent *entry;  while((Entry = Readdir (dir))! =NULL) {        //if (Entry->d_type = = 4) continue;//It ' s dircout <<"name ="<< Entry->d_name <<", Len ="<< Entry->d_reclen <<", Entry->d_type ="<< (int) Entry->d_type <<Endl; stringName = Entry->D_name; stringImgdir = Gfileloadpath +name; Mat img=Imread (Imgdir.c_str ()); Imshow (Entry-D_name, IMG); stringResultdir = Gfiledstpath +name;    Imwrite (RESULTDIR.C_STR (), IMG);    } closedir (dir); Waitkey (0); //System ("pause");    return 0;}

Read all files in the specified directory (Windows and Linux editions)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.