0

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

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by MrSpigot
0

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().

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.