How to compare two text files and display where the differences are a

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 9368 Views - Last Post: 30 December 2009 - 09:51 PM Rate Topic: -----

#1 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

How to compare two text files and display where the differences are a

Posted 24 December 2009 - 05:55 PM

Hi

I'm still learning C programming. But I'm trying to write a program that will compare two text files and then if the two files are different, then tell me where the differences are (like what line, paragraph, etc) and if possible, display the differences.

Using strcmp to compare two text files is easy. It's the other parts that I'd like to know how to be able to do...

My attempt below....

I'm trying to now compare two files, then have program pick up the strings, then if different, display what the strings are. At least, that's my logic if it works.

//compare two files, then report where the differences are.

#include "stdafx.h"
#include <stdio.h>
#include<string.h>

int main()
{
	char c1[1000], c2[1000];
	//char s1[1000], s2[1000];
	FILE *f1;
	FILE *f2;

	f1 = fopen ("output.txt", "r");
	f2 = fopen ("output_copy.txt", "r");

	fgets(c1, 1000, f1);
	fgets(c2, 1000, f2);

	printf("\n");
	
	while((fgets(c1,1000,f1)!= NULL) && (fgets(c2,1000,f2)!= NULL))
	{
		if(strcmp(c1, c2) != 0)
			printf("%s", c1);
			printf("%s", c2);
	}
	printf("\n");

	fclose(f1);
	fclose(f2);
	return 0;
}



Thanks in advance!! :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to compare two text files and display where the differences are a

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 603
  • View blog
  • Posts: 1,864
  • Joined: 02-August 09

Re: How to compare two text files and display where the differences are a

Posted 24 December 2009 - 07:21 PM

Whats the problem? You dont seem to mention any problems with this code.
Was This Post Helpful? 0
  • +
  • -

#3 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

Re: How to compare two text files and display where the differences are a

Posted 24 December 2009 - 10:39 PM

the first problem ... nothing is being printed, I thought it should work unless I made a coding error.

second, how to get the program to tell me where exactly the files differ?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: How to compare two text files and display where the differences are a

Posted 24 December 2009 - 11:42 PM

is there only one line in any of those files ?

x2x3i5x said:

second, how to get the program to tell me where exactly the files differ?

what does it mean ? a byte ? or a number of the line and the character's position ?
Was This Post Helpful? 0

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: How to compare two text files and display where the differences are a

Posted 25 December 2009 - 01:21 AM

EDIT: sorry, it's C programming. try looking up a page on C file IO ?
You're probably going to want to output the location of the C equivalent to the GET pointer in C++ file io. I don't think I have any C examples handy. Sorry, and merry christmas!

Does this help any?

hmm.. I think if you output the location of the pointer in the file stream as soon as a difference is found it will work, correct?
You can have this code.

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
class Compare
{
private:
	HANDLE hfile1,hfile2;
	char * buffer;
	char * bufferF2;
public:
	BOOL rTestF1,rTestF2;
	DWORD readcF1,readcF2;
	Compare()
	{
	buffer=NULL,bufferF2=NULL;
	buffer=new char[1024];
	bufferF2=new char[1024];
	rTestF1=true;
	rTestF2=true;
	readcF1=1;
	readcF2=1;
	}
	~Compare()
	{
			//cout << "Object Destroyed. " << endl;
			delete [] buffer;
			delete [] bufferF2;
			CloseHandle(hfile1);
			CloseHandle(hfile2);
	}
	int Open(char * arg1,char* arg2)
	{
		hfile1=CreateFileA(arg1,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
		hfile2=CreateFileA(arg2,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
		if(hfile1==INVALID_HANDLE_VALUE || hfile2==INVALID_HANDLE_VALUE)
		{
			cout << "Error code " << GetLastError() << endl;
			return 1;
		}
		else
			return 0;
	}

private:
	int Read()
	{
		rTestF1=ReadFile(hfile1,buffer,1024,&readcF1,0);
		rTestF2=ReadFile(hfile2,bufferF2,1024,&readcF2,0);
		if(rTestF1==0 || rTestF2==0 )
		{
			cout << "Error code " << GetLastError() << endl;
			return 1;
		}
		else
		{
			return 0;
		}
	}
public:
	int Cmp()
	{
		while(Read()==0 && readcF1!=0 && readcF2!=0)
		{
			for(unsigned long a=0;a<readcF1;a++)
		{
			if(buffer[a]!=bufferF2[a])
			{
				cout << "Files are not equal!\n";
				//~Compare();
				return 1;
			}
		}
		}
		cout << "Files are equal.\n";
		return 0;
	}
};


This post has been edited by taylorc8: 25 December 2009 - 01:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

Re: How to compare two text files and display where the differences are a

Posted 29 December 2009 - 03:20 PM

Thank you...

Here's what I did have ...

///compare two files, then report where the differences are.

#include "stdafx.h"
#include <stdio.h>
#include<string.h>

int main()
{
	char c1[1000], c2[1000];
	//char s1[1000], s2[1000];
	FILE *f1;
	FILE *f2;

	f1 = fopen ("output.txt", "r");
	f2 = fopen ("output_copy.txt", "r");

	fgets(c1, 1000, f1);
	fgets(c2, 1000, f2);


	
	printf("\n");
	
	while((fgets(c1,1000,f1)!= NULL) && (fgets(c2,1000,f2)!= NULL))
	{
		if(strcmp(c1, c2) != 0)
			puts(c1);
			printf("\n");
			puts(c2);
	}
	
	printf("\n");

	fclose(f1);
	fclose(f2);
	return 0;
}




1. It prints out string when they're not the same (At least that's what I want it to do, but it's not exactly doing it...)

This post has been edited by x2x3i5x: 29 December 2009 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#7 Guest_c.user*


Reputation:

Re: How to compare two text files and display where the differences are a

Posted 29 December 2009 - 05:48 PM


#include <stdio.h>
#include <string.h>

int main(void) /* C89 ANSI */
{
	char line1[100] = "abcd abcd\n",
		 line2[100] = "abcd efgh\n";
	/* example lines */
	
	long lineno;
	/* a lines counter */
	
	for (lineno = 0; ; ) {
		
		lineno++;
		/* increment the line counter */
		
		if (strcmp(line1, line2) != 0) {
			long i, charno;
			
			charno = 0;
			/* a characters counter */
			
			for (i = 0; line1[i] != '\0'; i++) {
				 
				charno++;
				/* increment the line characters counter */
				 
				if (line1[i] != line2[i])
					break;
				/* comparing characters until the first
				   difference occures */
			}
			
			printf(
				"lines are different"
				" in line #%ld"
				" and char #%ld"
				"\n",
				lineno,
				charno
			);
			/* print a message with the corresponding
			   line number and character number */
			
			break;
			/* stop the line considering loop */
		}
	
	}
	
	return 0;
}


This post has been edited by c.user: 29 December 2009 - 05:49 PM

Was This Post Helpful? 0

#8 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

Re: How to compare two text files and display where the differences are a

Posted 29 December 2009 - 05:51 PM

Quote

1. 1. It prints out string when they're not the same (At least that's what I want it to do, but it's not exactly doing it...)


This is the problem I wanted to fix first, not the thing about the line count, because I was already getting somewhere closer. But thank you anyway for looking at the line count issue..

Instead of you writing out the code for me every time, I think it would be better if you helped me out with my code by giving suggestions or hints that may help me get where I wanted (or help me get better at programming). Somehow, I don't feel like having the code spoon fed to me every time, unless I was really stuck.

#include "stdafx.h"
#include <stdio.h>
#include<string.h>

int main()
{
	char c1[1000], c2[1000];
	//char s1[1000], s2[1000];
	FILE *f1;
	FILE *f2;

	f1 = fopen ("output.txt", "r");
	f2 = fopen ("output_copy.txt", "r");

	fgets(c1, 1000, f1);
	fgets(c2, 1000, f2);

	printf("\n");
	
	while((fgets(c1,1000,f1)!= NULL) && (fgets(c2,1000,f2)!= NULL))
	{
		if(strcmp(c1, c2) != 0)
			puts(c1);
			printf("\n");
			puts(c2);
	}
	
	printf("\n");

	fclose(f1);
	fclose(f2);
	return 0;
}


This post has been edited by x2x3i5x: 29 December 2009 - 09:50 PM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_c.user*


Reputation:

Re: How to compare two text files and display where the differences are a

Posted 30 December 2009 - 12:52 PM

	if(strcmp(c1, c2) != 0)
		puts(c1);
		printf("\n");
		puts(c2);



	if(strcmp(c1, c2) != 0) {
		puts(c1);
		printf("\n");
		puts(c2);
	}



x2x3i5x said:

Somehow, I don't feel like having the code spoon fed to me every time

you don't know in syntax yet

	fgets(c1, 1000, f1);
	fgets(c2, 1000, f2);

...

	while((fgets(c1,1000,f1)!= NULL) && (fgets(c2,1000,f2)!= NULL))



	/* fgets(c1, 1000, f1); */ /* should be removed both */
	/* fgets(c2, 1000, f2); */

...

	while(fgets(c1,1000,f1) != NULL && fgets(c2,1000,f2)!= NULL)



these to fgets calls above the while loop tell me you don't know that the while loop doesn't consider them
thus, I had to fed you by the code
almost in every line :)

This post has been edited by c.user: 30 December 2009 - 12:58 PM

Was This Post Helpful? 0

#10 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

Re: How to compare two text files and display where the differences are a

Posted 30 December 2009 - 01:23 PM

1.

Quote

You don't know in sytax
?

What is "in syntax"? Syntax for what ?

2. I meant, I'd like help improving my code to get what I would like to do, not you having to write a brand new code to do what I would like to do. Thank you for point out that fgets error, I missed it. But I forgot to remove the while loop, that's what should be deleted. The while loop got me nowhere.

3. Right now, the code prints out the string it read when it's different. But it prints out everything including the differences. So if one file has "Text Text" and the other has "Text Test", it'll print out everything.

Also Text Text is not being read the same Text
Text


So basically, I'd like the program to print out the the words that are different, regardless of where it is in the text files. Hope this makes sense?

Thanks for the help!! :)

This post has been edited by x2x3i5x: 30 December 2009 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#11 Guest_c.user*


Reputation:

Re: How to compare two text files and display where the differences are a

Posted 30 December 2009 - 02:26 PM

x2x3i5x said:

What is "in syntax"? Syntax for what ?

yes, brackets (round, what they do), blocks (that I fixed above)
these are so beginner errors
if you don't know in blocks hence you don't know in scopes
it would not wonder for me if you would make something erroneous with it
I know you don't know in operations (&& || !) how they work
If you will try something complicated with || I sure you will get wrong result

x2x3i5x said:

But it prints out everything including the differences.

I know you don't know pointers which could solve it easy

I can tell you "use pointers" but how does it help

This post has been edited by c.user: 30 December 2009 - 04:26 PM

Was This Post Helpful? 0

#12 Guest_c.user*


Reputation:

Re: How to compare two text files and display where the differences are a

Posted 30 December 2009 - 02:33 PM

x2x3i5x said:

So basically, I'd like the program to print out the the words that are different


look at this

Text text
Text	 text


words are equal, buf files are different

This post has been edited by c.user: 30 December 2009 - 02:37 PM

Was This Post Helpful? 0

#13 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

Re: How to compare two text files and display where the differences are a

Posted 30 December 2009 - 02:54 PM

...

This post has been edited by x2x3i5x: 30 December 2009 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#14 Guest_c.user*


Reputation:

Re: How to compare two text files and display where the differences are a

Posted 30 December 2009 - 04:30 PM

it may differ also like

"abcd" and "abcd\n"
how does it have to be printed, '\n' is not printable
that's why the number of the line and the number of the character would be rather
then you can determine whether the character is printable and optionally print it out or not

This post has been edited by c.user: 30 December 2009 - 04:36 PM

Was This Post Helpful? 0

#15 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

Re: How to compare two text files and display where the differences are a

Posted 30 December 2009 - 05:01 PM

Now, what would you do to store characters from a string read out of a text into an array?

I was thinking you have characters stored in an array pulled out of the strings read from the text (do the storing into the array when a difference is detected and then stop when it stops being different), then you could print out the characters one by one .... ?

This post has been edited by x2x3i5x: 30 December 2009 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2