Help

Jul 10, 2015 at 9:12am
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;
}
Jul 10, 2015 at 9:26am
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.
Jul 10, 2015 at 10:25am
i need this
exemple
txt1
line 1
txt2
line 1
line 1
output
line1

only time no 2 times
Last edited on Jul 10, 2015 at 10:26am
Jul 10, 2015 at 11:22am
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))
		{
...
Jul 13, 2015 at 7:32am
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



Jul 13, 2015 at 7:57am
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 Jul 13, 2015 at 12:02pm
Jul 13, 2015 at 11:35am
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.
Jul 13, 2015 at 12:38pm
I find it txt dhydan ;)
Topic archived. No new replies allowed.