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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <utility>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
using namespace std;
struct appLine_1
{
std::string word_1;
std::string word_2;
std::string word_3;
std::string word_4;
appLine_1()
{}
appLine_1(const std::string& _word_1, const std::string& _word_2, const std::string& _word_3, const std::string& _word_4):
word_1(_word_1), word_2(_word_2), word_3(_word_3), word_4(_word_4)
{}
};
struct appLine_2
{
std::string word_1;
std::string word_2;
std::string word_3;
std::string word_4;
std::string word_5;
std::string word_6;
appLine_2()
{}
appLine_2(const std::string& _word_1, const std::string& _word_2, const std::string& _word_3, const std::string& _word_4, const std::string& _word_5
, const std::string& _word_6):
word_1(_word_1), word_2(_word_2), word_3(_word_3), word_4(_word_4), word_5(_word_5), word_6(_word_6)
{}
};
void readMyFile_1(vector<appLine_1>& apndLine_1);
void readMyFile_2(vector<appLine_2>& apndLine_2);
void writeToFile(vector<appLine_1>& apndLine_3);
int main()
{
vector<appLine_1> apndLine_1;
vector<appLine_2> apndLine_2;
vector<appLine_1> apndLine_3;
readMyFile_1(apndLine_1);
readMyFile_2(apndLine_2);
appLine_1 apLn_1;
for(std::vector<appLine_1>::iterator i = apndLine_1.begin(); i != apndLine_1.end(); i++)
{
apLn_1.word_1 = i->word_1;
apLn_1.word_2 = i->word_2;
apLn_1.word_3 = i->word_3;
apLn_1.word_4 = i->word_4;
apndLine_3.push_back(apLn_1);
for(std::vector<appLine_2>::iterator j = apndLine_2.begin(); j != apndLine_2.end(); j++)
{
if((i->word_4 == j->word_5) && (i->word_2 == j->word_6))
{
apLn_1.word_1 = "this, ";
apLn_1.word_2 = "too, ";
apLn_1.word_3 = "is, ";
apLn_1.word_4 = "cool";
apndLine_3.push_back(apLn_1);
}
}
}
writeToFile(apndLine_3);
for(std::vector<appLine_1>::iterator i = apndLine_3.begin(); i != apndLine_3.end(); i++)
{
cout << "apndLine_3: " << i->word_1 << i->word_2 << i->word_3 << i->word_4 << endl;
}
return 0;
}
void readMyFile_1(vector<appLine_1>& apndLine_1)
{
ifstream in("myfile_1.txt");
string line;
appLine_1 apLn_1;
while(getline(in, line))
{
istringstream ss(line);
getline(ss,apLn_1.word_1,',');
getline(ss,apLn_1.word_2,',');
getline(ss,apLn_1.word_3,',');
getline(ss,apLn_1.word_4);
apndLine_1.push_back(apLn_1);
}
}
void readMyFile_2(vector<appLine_2>& apndLine_2)
{
ifstream in("myfile_2.txt");
string line;
appLine_2 apLn_2;
while(getline(in, line))
{
istringstream ss(line);
getline(ss,apLn_2.word_1, ',');
getline(ss,apLn_2.word_2, ',');
getline(ss,apLn_2.word_3, ',');
getline(ss,apLn_2.word_4, ',');
getline(ss,apLn_2.word_5, ',');
getline(ss,apLn_2.word_6, ',');
apndLine_2.push_back(apLn_2);
}
}
void writeToFile(vector<appLine_1>& apndLine_3)
{
ifstream in("output.txt");
ofstream myfile;
myfile.open ("output.txt");
for(int i = 0; i < apndLine_3.size(); i++)
{
myfile << apndLine_3[i].word_1 + ',';
myfile << apndLine_3[i].word_2 + ',';
myfile << apndLine_3[i].word_3 + ',';
myfile << apndLine_3[i].word_4;
myfile << '\n';
}
myfile.close();
}
|