help comparing file input using getline()

I'm don't fully understand how im supposed to compare the two different files, i got the program to read in the txt file and give an error if its not in the folder but then i got stuck heres the prolem

Write a main function that performs the following
operations:

a.) Prompts the user to input the names of 3 files: two input files and
an output file.

b.) Reads in the two input files line by line and compares the two
lines.

c.) For each line in the inputs, the output should have the line number,
a colon (':'), and either "OK" if the lines match or "DIFF" if the lines
are different.

ex)input1:
abc
def
g h i

input2:
abc
DEf
ghi
uub

output:
1:OK
2:DIFF
3:DIFF
4:DIFF

heres my code so far

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
#include "std_lib_facilities_3.h"

int main()
{ 	
	string file1;
	string file2;
	string out_file;
	
	{
	cout<<"Please enter the name of file 1\n";
	getline(cin,file1);
	ifstream ist(file1.c_str()); 
	if(!ist) error("can't open input file ",file1);
	}
	
	{
	cout<<"Please enter the name of file 2\n";
	getline(cin,file2);
	ifstream ist(file2.c_str()); 
	if(!ist) error("can't open input file ",file2);
	}
	
	{
	cout<<"Please enter the name of ouput file\n";
	getline(cin,out_file);
	ofstream ost(out_file.c_str()); 
	if(!ost) error("can't open ouput file ",out_file);
	
	ost<<"1:"<< <<"\n";
	ost<<"2:"<< <<"\n";
	ost<<"3:"<< <<"\n";
	ost<<"4:"<< <<"\n";
	}
	
	return 0;
}
	
Last edited on
I would imagine that you need to read each line from each file and check to see if they are different.

check out strcmp();

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
i understand the concept but how do you tell the program to differentiate between the multiple lines of letters in each file
It depends on how you read the file. if you use ifstream.getline(), the file stream stops reading at the first '\n' character, or the end of the stream. it then moves the file pointer.
psuedo code:

While file 1 has text, read each line until end of file.
Push each line (std::string) into a vector of strings... vector<string>
Close file 1

While file 2 has text, read each line until end of file.
Push each line (std::string) into a vector of strings... vector<string>
Close file 2

for each element in the vectors, until the end of the smallest vector, compare each string for that index...
if(vector1[index] == vector2[index]) Print out depending on that comparison. (std output or file)

If the vectors are not the same length(1 file is larger than the other), then the remaining lines automatically fall into the "DIFF" catagory.
You don't need vector<>s for this homework. You do, however, need to create those file streams outside of local blocks. (Try getting rid of lines 9, 14, 16, 21, etc. Notice that you will have to rename the ifstreams: something like ist1 and ist2 maybe?)

You will also need to use a loop; do not assume that both files have exactly four lines of text.

Good luck!
alright made two vectors but still fairly confused on comparing lines
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
#include "std_lib_facilities_3.h"

int main()
{ 	
	string file1;
	string file2;
	string out_file;
	string input1;
	string input2;
	vector<string>vector1;
	vector<string>vector1;
	int vect1_length;
	int vect2_length;
	
	cout<<"Please enter the name of file 1\n";
	getline(cin,file1);
	ifstream ist1(file1.c_str()); 
	if(!ist1) error("can't open input file ",file1);
	if(ist1.is_open())
	{
		while(ist1.good())
		{
		getline(ist1.input1);
		vector1.push_back(input1);
		}	
		ist1.close();	
	}
	
	
	cout<<"Please enter the name of file 2\n";
	getline(cin,file2);
	ifstream ist2(file2.c_str()); 
	if(!ist2) error("can't open input file ",file2);
	if(ist2.is_open())
	{
		while(ist2.good())
		{
		getline(ist2.input2);
		vector2.push_back(input2);
		}	
		ist2.close();	
	}
	
	
	cout<<"Please enter the name of ouput file\n";
	getline(cin,out_file);
	ofstream ost(out_file.c_str()); 
	if(!ost) error("can't open ouput file ",out_file);
	
	vect1_length=vector1.size();
	vect2_length=vector2.size();
	if(vector1[index1]==vector2[index1])
		
		
	return 0;
}
	
I purposely only showed the comparison. You need a loop of some sort. Using either iterators or an indices. Right? Because you need to iterate over each element, comparing each one (index version)if(vector1[index1]==vector2[index1]) or (iterator version)if(*itr1==*itr2), then depending on that comparison you take an action. By itself, the comparison is useless, as you can see from your code.

for example. Lets say I have two variable and I want to know which one is larger.

1
2
3
4
5
int a(5), b(10);
if(a < b) //Here is the comparison, same as the vector1[index]==vector2[index]
   cout << "A is less than B" << endl;  //action if true
else
   cout << "A is not less than B" << endl;  //action if false 


You need to implement the action for your assignment based on the DIFF/SAME or whatever it is.
Last edited on
alright updated and its working to a certain it only reads up to shortest txt file line and still didn't count the abc and abc as ok but saw them as different

test is just to show which if statement it goes into

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
#include "std_lib_facilities_3.h"

int main()
{ 	
	string file1;
	string file2;
	string out_file;
	string input1;
	string input2;
	vector<string>vector1;
	vector<string>vector2;
	int vect1_length;
	int vect2_length;
	int counter=1;
	
	cout<<"Please enter the name of file 1\n";
	getline(cin,file1);
	ifstream ist1(file1); 
	if(!ist1) error("can't open input file ",file1);
	if(ist1.is_open())
	{
		while(ist1.good())
		{
			getline(ist1,input1);
			vector1.push_back(input1);
		}	
		ist1.close();	
	}
	
	
	cout<<"Please enter the name of file 2\n";
	getline(cin,file2);
	ifstream ist2(file2.c_str()); 
	if(!ist2) error("can't open input file ",file2);
	if(ist2.is_open())
	{
		while(ist2.good())
		{
		getline(ist2,input2);
		vector2.push_back(input2);
		}	
		ist2.close();	
	}
	
	
	cout<<"Please enter the name of ouput file\n";
	getline(cin,out_file);
	ofstream ost(out_file.c_str()); 
	if(!ost) error("can't open ouput file ",out_file);
	
	vect1_length=vector1.size();
	vect2_length=vector2.size();
	cout<<vect1_length<<"\n";
	cout<<vect2_length<<"\n";
	if(vect1_length==vect2_length)
	{
		cout<<"test1\n";
		
		for(int x=0;x<vect1_length;x++)
		{
			if(vector1[x]==vector2[x])
			{
				ost<<counter<<":OK\n";
				counter++;
			}
			else
			{
				ost<<counter<<":DIFF\n";
				counter++;
			}
		}
	}
	
	if(vect1_length>vect2_length)
	{
		cout<<"test2\n";
		
		for(int x=0;x<vect2_length;x++)
		{
			if(vector1[x]==vector2[x])
			{
				ost<<counter<<":OK\n";
				counter++;
			}
			else
			{
				ost<<counter<<":DIFF\n";
				counter++;
			}
		}
	}
	
	if(vect1_length<vect2_length)
	{
		cout<<"test3\n";
		
		for(int x=0;x<vect1_length;x++)
		{
			if(vector1[x]==vector2[x])
			{
				ost<<counter<<":OK\n";
				counter++;
			}
			else
			{
				ost<<counter<<":DIFF\n";
				counter++;
			}
		}
	}
		
		
	return 0;
}
	
...and this is why I hate responding to threads like this...

because the OP, knowing very little, will often follow advice that is against his best interests, presented as a simple, follow the example solution without all the relevant caveats.

@loredo
You are wasting your time and energy playing with vector<>s. What started as a very simple homework has been deviated into a much larger project than it need be, with you finding yourself much more confused than at the start. If you turn that code in, you will fail.

Get rid of the *&#! vectors and do exactly what your homework asks:

1) open three files: two for input and one for output.
2) while you can read lines (one from each input file):
3) compare the two lines and write a response to the output file

Your homework is not to rewrite diff(1) or any other very advanced pattern matching program.
Topic archived. No new replies allowed.