954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String comparison in two files

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

guest7
Junior Poster
109 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

Does that even compile for you ?

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

MrSpigot
Junior Poster
158 posts since Mar 2009
Reputation Points: 76
Solved Threads: 40
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You