Help

My code do this
ex1
txt1
line 1
txt2
line1
out put
line 1

is ok but if I have
txt1
line 1
txt2
line 1
line 1
output
line1
line1

I need 1 time in output...help



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
  #include <fstream>    
#include <iostream>
#include <string>
using namespace std;



int main(int argc, char* argv[])
{
	ifstream input;
	ifstream compare;
	ofstream output;
	string line_input, line_compare;

	input.open(argv[1]);
	output.open(argv[3]);

	while (!input.eof())
	{
		getline(input, line_input);

		compare.open(argv[2]);
		while (!compare.eof())
		{
			getline(compare, line_compare);


			if (line_input == line_compare)
			{
				output << line_input << endl;
			}
		}
		compare.close();

	}

	cout << endl;
	input.close();
	output.close();

	system("pause");
	return 0;
}
It is like merge + unique:
http://en.cppreference.com/w/cpp/algorithm/merge
http://en.cppreference.com/w/cpp/algorithm/unique_copy
Or set intersection ( http://en.cppreference.com/w/cpp/algorithm/set_intersection ) depending on other unknown requirements of your assigment.

If your ranges are not sorted, you will have to store file data in your program. There is no on-line way to do that.
i need this
exemple
txt1
line 1
txt2
line 1
line 1
output
line1

only time no 2 times
Last edited on
You get the duplicates due to this:
1
2
3
4
5
6
7
8
9
while (!input.eof())
	{
		getline(input, line_input); // continues even ef getline(...) detects the eof

		compare.open(argv[2]);
		while (!compare.eof())
		{
			getline(compare, line_compare); // continues even ef getline(...) detects the eof
...

change to
1
2
3
4
5
6
while (getline(input, line_input)) // getline(...) returns [indirectly] true if anything is ok and false otherwise (eof)
	{
		compare.open(argv[2]);
		while (getline(compare, line_compare))
		{
...
It is not good
if I wont this

txt1
line 1
line 1
line 2
line 1

txt2
line 1
line 1
line 1
line 1

output
line 1
line 1
line 1
line 1
line 1
line 1

to many line help



According to your original post it seems that you want the output only if the line exists in both file, correct?

If so make two not nested loops.

First loop: Read the lines of the first file into a vector.

http://www.cplusplus.com/reference/vector/vector/?kw=vector


Second loop: Read line by line of the second file. Find the line in the vector.

http://www.cplusplus.com/reference/algorithm/find/?kw=find

If found (i.e. != input_vector.end()) -> output.

So change your code into two not nested loops. Add a vector for the first loop and use find for the if on line 28. Oh, and still avoid the duplicates.
Last edited on
I think coder777's algorithm will still output duplicate lines if they appear in the second file. How about this:
First loop: read the lines of txt1 into a set (a set provides faster lookup than a vector)

Second loop: read txt2 line by line. Try to find it in the set.
if you find it then {
output the line
remove the line from the set
}

Removing the line from the set will prevent duplicates from appearing.
I find it txt dhydan ;)
Topic archived. No new replies allowed.