#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);
}
获取文件夹中指定后缀文件路径,并输入到文本文件中
最新推荐文章于 2023-09-30 16:36:43 发布