Hello everyone Could anyone please help me how do I insert text between lines of a files depending on certain condition? Thanks
This program is working but I am not sure if it is an efficient way of doing it. Could anyone please suggest. Code: #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(); }
You cannot insert data into files because file I/O does not support that. So you have to rewrite the file. Read it in line by line, write each line to the new file, and if the condition is TRUE then write the new line, then continue with the next line from the original file. Then you can delete the old file and rename the new file to the old name, thus giving the impression that data has been inserted.