搜索文件夹中某类型的文件

#include <iostream>
#include <dirent.h>
#include <map>
#include <fstream>
#include <iomanip>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

vector<string> temp;

int main(int argc,char *argv[]){
	void findFiles(const char *path);

	if (argc!=2){
		printf("a single argument is required\n");
		return 0;
	}

	if(argv[1][strlen(argv[1])-1]=='/')
		argv[1][strlen(argv[1])-1]='\0';

	findFiles(argv[1]);

	vector<string>::iterator it;
	ofstream ofile;
	ofile.open("myfile.txt");
	int m,n,i;

	for(it=temp.begin();it!=temp.end();it++)
		ofile<<*it<<endl;
	ofile.close();

	return 0;
}

void findFiles(const char *path){
	DIR *dir;
	struct dirent *dirp;
	stringstream stream;
	VideoCapture capture;

	if((dir=opendir(path))==NULL){
		printf("can't open %s",path);
		exit(0);
	}
	while ((dirp=readdir(dir))!=NULL){
		if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)//current dir OR parrent dir
			continue;
		else if(dirp->d_type == 8 || dirp->d_type == 10){//file OR link file
			stream<<path<<"/"<<dirp->d_name;
			if(stream.str().substr(stream.str().size()-3,3)==".so"){//delete this line will find all files
				temp.push_back(stream.str());
			}
			stream.str("");
		}
	}
	dir=opendir(path);
	while ((dirp=readdir(dir))!=NULL){
		if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)//current dir OR parrent dir
			continue;
		else if(dirp->d_type == 4){//dir
			stream.str("");
			stream<<path<<"/"<<dirp->d_name;
			findFiles(stream.str().c_str());
		}
	}
	closedir(dir);
}

猜你喜欢

转载自blog.csdn.net/qq_26697045/article/details/86559586