1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <string>
#include<iostream>
#include <fstream>
#include <sstream>
std::string mid(std::string str, int pos1, int length)
{
int i;
pos1-=1;
std:: string temp = "";
if (pos1<0){return str;};
for(i = pos1; i < pos1+length; i++)
{
if (i>str.length()-1) {return temp;};
temp += str[i];
}
return temp;
}
int instr(int n,std::string istring_,std:: string tofind){
std::string istring=mid(istring_,n,istring_.length());
if (istring.find(tofind)!=istring.npos){
return istring.find(tofind)+1;
}else{
return 0;
}
}
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
template <typename L>
void load(const char * filename,L &array)
{
int s=filesize(filename)/sizeof(array[0]);
array.resize(s);
FILE * f = fopen(filename, "rb");
auto ap = &array[0];
fread(ap,array.size()*sizeof(array[0]),1,f);
fclose(f);
}
int main()
{
int p1=0,p2=0;
std::string s,g;
std::string filename;
for(int i=1;i<4;i++) // create test files
{
if (i==1) filename= "test.xml";
if (i==2) filename= "test1.xml";
if (i==3) filename= "test2.xml";
std::ofstream( filename ) <<
R"(<REUTERS ... >
<DATE>26-FEB-1987 15:01:01.79</DATE>
<TOPICS><D>cocoa</D></TOPICS>
<PLACES><D>el-salvador</D><D>usa</D><D>uruguay</D></PLACES>
<PEOPLE></PEOPLE>
<ORGS></ORGS>
<EXCHANGES></EXCHANGES>
<COMPANIES></COMPANIES>
<UNKNOWN> ... </UNKNOWN>
<TEXT> ...
<TITLE>BAHIA COCOA REVIEW</TITLE>
<DATELINE> SALVADOR, Feb 26 - </DATELINE>
<BODY>Showers continued throughout
the week in the Bahia cocoa zone, alleviating the drought since
...
...
Brazilian Cocoa Trade Commission after
carnival which ends midday on February 27.
Reuter
</BODY></TEXT>
</REUTERS>
)" ;
}
system("dir /b > files.txt");
load("files.txt",g);
std::istringstream dir(g);
std::string t;
while ( dir>>t)
{
load( t.c_str(),s);
std::cout<<"file:"<<t.c_str()<<std::endl;
p1=instr(1,s,"<BODY>")+6;// 6=length of <BODY>
p2=instr(1,s,"</BODY>");
std::string ans=mid(s,p1,p2-p1);
std::cout<<ans<<std::endl<<std::endl;
}
system("pause");
}
| |