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
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
//======================================================================
void getWholeFile( string filename, string &buffer ) //Get the whole file, replacing line breaks by a space
{
ifstream in( filename ); assert( in );
string line;
while ( getline( in, line ) ) buffer += line + " ";
in.close();
}
//======================================================================
void extractData( const string& buffer, string prefix, string suffix, vector<string> &allData ) // Extract data from buffer
{
size_t p = 0, q = 0;
while( true )
{
p = buffer.find( prefix, p ); if ( p == string::npos) break;
p += prefix.size();
q = buffer.find( suffix, p );
allData.push_back( buffer.substr( p, q - p ) );
p = q + suffix.size();
}
}
//======================================================================
void output( const vector<string> &allData, const string &filename )
{
ofstream out( filename );
int lineNum = 0; // just in case ...
for ( string line : allData )
{
lineNum++; // still just in case you change your mind ...
out << "+1 ";
stringstream ss( line );
int n = 1, value;
while ( ss >> value )
{
out << n << ':' << value << " ";
n++;
}
out << endl;
}
out.close();
}
//======================================================================
int main()
{
vector<string> allData1, allData2;
string buffer1, buffer2;
string infile1 = "input1.xml", prefix1 = "<BinCounts>", suffix1 = "</BinCounts>";
string infile2 = "input2.xml", prefix2 = "<Values>", suffix2 = "</Values>";
string outfile = "output.dat";
getWholeFile( infile1, buffer1 );
extractData( buffer1, prefix1, suffix1, allData1 );
getWholeFile( infile2, buffer2 );
extractData( buffer2, prefix2, suffix2, allData2 );
// Combine corresponding lines into the strings of allData1 - assumes equal numbers
assert( allData1.size() == allData2.size() ); // Taking no chances
for ( int i = 0; i < allData1.size(); i++ ) allData1[i] += " " + allData2[i];
output( allData1, outfile );
}
//======================================================================
|