Hi,

I am comparing two file to find if they have common strings. My program is not doing that and I cannot figure out the reason. Any help is appreciated. Following is my code:

int main()
{
	FILE *inf1 = fopen("file1.txt","r");
	FILE *inf2 = fopen("file2.txt","r");
	int numcols = 11;
	char line1[numcols], line2[numcols];

	while(fgets(line1, (numcols+1), inf1))
	{
		while(fgets(line2, (numcols+1), inf2))
		{
			if(line1 == line2)
			{
				cout << "Found a matching line" << "line1 = " << line1 << endl;
				break;
			}
		}
		rewind(inf2);
	}
	return 0;
}

Following are my file1.txt and file2.txt

file1.txt
---------------
1 0 2 2 0 1
1 1 2 2 1 1

file2.txt
-------------
0 0 0 0 0 0
0 0 0 0 0 1
1 0 2 2 0 1
1 1 2 2 1 1

Thanks

>>int numcols = 11;
>> char line1[numcols], line2[numcols];

Does that even compile for you ?

Assuming you can get it to compile, your string comparison in:
if(line1 == line2)
is actually comparing the addresses of line1 and line2, not the array contents.
Try using string objects to store the lines, or use an array comparison method such as strcmp().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.