64,282
社区成员
发帖
与我相关
我的任务
分享
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <istream>
#include <io.h>
using namespace std;
string sub_s;
void search(vector<string> & lines,string key);
void GetAllFormatFiles(string path,vector<string>& files,string format);
int main(int argc, char* argv[])
{
string filePath = "e:\\MP";
vector<string>files;
char * distAll = "AllFiles.txt";
string format = ".txt";
GetAllFormatFiles(filePath,files,format);
int size = files.size();
int i;
for( i = 0; i < size;i++)
cout << files[i] << endl;
ifstream in(files[i]);
if(!in)
{
cerr << "文件读取错误" << endl;
system("pause");
return 1;
}
vector<string>lines;
string line;
const char* target = "#START:CONNECTION_FASTBOOT_CHECK";
while(getline(in,line))
{
lines.push_back(line);
}
search(lines,target);
ofstream out ("2.txt",ios::app);
if(!out)
{
cerr << "failed " << endl;
system("pause");
return 1;
}
out << sub_s << endl;
return 0;
}
void search(vector<string> & lines,string key)
{
for(int i = 0;i < lines.size(); ++i)
{
if(lines[i].find(key) != string::npos)
{
sub_s = lines[i+1].substr(23,8);
cout << sub_s << endl;
}
}
}
void GetAllFormatFiles(string path,vector<string>& files,string format)
{
long hFile = 0;
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*"+ format).c_str(),&fileinfo)) != -1)
{
do
{
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,"." )!= 0 && strcmp(fileinfo.name,"..")!=0)
{
GetAllFormatFiles(p.assign(path).append("\\").append(fileinfo.name),files,format);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
}while(_findnext(hFile,&fileinfo)==0);
_findclose(hFile);
}
}