insert text between lines of file

Hello everyone

Could anyone please help me how do I insert text between lines of a files depending on certain condition?

Thanks
Read the whole file into a vector<string>.
Insert your text.
Write the whole file back to disk.
Thanks Duoas

there are actually two files. I have to read both files and insert data from one file to another if certain conditions are met. E-g if a line in file-1 consists of text "A" and a line in file-2 also consists of text "A" then append the whole line from file-2 to file-1 beneath the line (in file-1) where the text "A" matches.

Could you please guide.

Thanks
It isn't that much different.

Open both files.
Read a line from both. (That means you'll have two lines in memory.)
Append the first line to your vector.
If the condition is satisfied, append the second line to the vector also.
Repeat until EOF.
Close both files.
When done, write the vector to file.
thanks Duoas

I have now got two struct type of vectors where one contains data from file-1 and the other contains data from file-2. The two files contain comma separated values. The structs contain string type of variables.
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
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;
    	std::string word_7;

	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):
			word_1(_word_1), word_2(_word_2), word_3(_word_3), word_4(_word_4), word_5(_word_5)
	{}
};


and that's how they are populated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);

//===============================

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, ',');
getline(ss,apLn_2.word_7, ',');

apndLine_2.push_back(apLn_2);


Now if you could please tell me (for example) how do I compare "word-1 of vector apndLine_1" to "word-3 of vector apndLine_2" AND "word-3 of vector apndLine_1" to "word-7 of vector apndLine_2" for equality i-e if they are the same words. If they are equal to each other then add that (similar) element of "vector apndLine_2" to the "vector apndLine_1" right after the element (of "vector apndLine_1") where the (similar) elements were found.

Many Thanks
Is this a correct way of comparison elements of two vectors for equality?

1
2
3
4
5
6
7
8
9
10
for(std::vector<appLine_1>::iterator i = apndLine_1.begin(); i != apndLine_1.end(); i++)
{
		for(std::vector<appLine_2>::iterator j = apndLine_2.begin(); j != apndLine_2.end(); j++)
		{
			if((i->word_1 == j->word_3) && (i->word_3 == j->word_7))
			{
				cout << "similar word: " << i->word_1 << " AND " << j->word_3 << endl;
			}
		}
}
and how do I find the index of that element? The below line of code is not working for a struct type of an element.

 
index = std::find(apndLine_1.begin(), apndLine_1.end(), i->word_4 ???) - apndLine_1.begin();
This program is now working but I am not sure if it is an efficient way of doing it. Could you please suggest.

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();
}
Topic archived. No new replies allowed.