获取文件夹中指定后缀文件路径,并输入到文本文件中

#include <vector>
#include <string>
#include <iostream> //for cout
#include <io.h>     //for _finddata_t、 _findnext
using namespace std;

typedef struct gcpInFile {
	char* tag = NULL;
	char* blocksize = NULL;
	char* numOfBlks = NULL;
	char* searchRANGE = NULL;
	char* oriIMAGE = NULL;
	char* outRESULT = NULL;
	char* DEM = NULL;
	char* DOM = NULL;
	char* numOfRefImages = NULL;
	char* DEFAULT = NULL;
};

vector<string>  getFilesList(string dir);
void getAllGcpInFile(string perPath,char* dir, vector<gcpInFile>&AllGcpInInfo);
int main(int argc,char * argv[])
{

	string dir;
	cout << "Enter a directory: ";
	dir = argv[1];
	//cin.getline(dir, 200);
	vector<string>allPath = getFilesList(dir);
	vector<gcpInFile>AllGcpInInfo;
	char *outoutPath = new char[256];
	outoutPath = argv[1];
	cout << "输出所有文件的路径:" << endl;
	
	char *allgcptxt = new char[256];
	FILE *fpnew;
	sprintf(allgcptxt, "%s/AllGcpIn.txt", argv[1]);
	fpnew = fopen(allgcptxt,"w");
	for (int i = 0; i < allPath.size(); i++)
	{
		string  perPath = allPath.at(i);
		cout << perPath << endl;
		char *SigperPath = new char[256];
		SigperPath = (char*)perPath.data();
		fprintf(fpnew, "%s\n", SigperPath);
		//getAllGcpInFile(perPath,outoutPath,AllGcpInInfo);
	}
	fclose(fpnew);
	return 0;
}

vector<string> getFilesList(string dir)
{
	vector<string> allPath;
	// 在目录后面加上"\\*.*"进行第一次搜索
	string dir2 = dir + "\\*.gcpin";

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dir2.c_str(), &findData);
	if (handle == -1) {// 检查是否成功
		cout << "can not found the file ... " << endl;
		return allPath;
	}
	do
	{
		if (findData.attrib & _A_SUBDIR) //是否含有子目录
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;

		// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
		string dirNew = dir + "/" + findData.name;
		vector<string> tempPath = getFilesList(dirNew);
		allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小
		{
			string filePath = dir + "/" + findData.name;
			allPath.push_back(filePath);
			//cout << filePath << "\t" << findData.size << " bytes.\n";
		}
	} while (_findnext(handle, &findData) == 0);
	_findclose(handle);    // 关闭搜索句柄
	return allPath;
}

void getAllGcpInFile(string perPath, char* dir, vector<gcpInFile>&AllGcpInInfo) {

	char *filePath = new char[256];
	filePath = (char*)perPath.data();
	FILE *fp;
	fp = fopen(filePath,"r");
	gcpInFile file;
	file.tag = new char[256];
	file.blocksize = new char[256];
	file.numOfBlks = new char[256];
	file.searchRANGE = new char[256];
	file.oriIMAGE = new char[256];
	file.outRESULT = new char[256];
	file.DEM = new char[256]; 
	file.DOM = new char[256];
	file.numOfRefImages = new char[256];
	file.DEFAULT = new char[256];
	fscanf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", file.tag, file.blocksize, file.numOfBlks, file.searchRANGE, file.oriIMAGE, file.outRESULT, file.DEM, file.DOM, file.numOfRefImages, file.DEFAULT);
	AllGcpInInfo.push_back(file);
	fclose(fp);

	char *allgcptxt = new char[256];
	FILE *fpnew;
	sprintf(allgcptxt, "%s/AllGcpIn.txt", dir);
	fpnew = fopen(allgcptxt,"a+");
	fprintf(fpnew, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", file.tag, file.blocksize, file.numOfBlks, file.searchRANGE, file.oriIMAGE, file.outRESULT, file.DEM, file.DOM, file.numOfRefImages, file.DEFAULT);
	fclose(fpnew);

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值