Thread: Call for help
View Single Post
Old 2005-07-11, 13:30   #10
cjohnsto
 
Jun 2005

F16 Posts
Default

How about the following ANSI C code for comparisons. Sorry about the lack of comments, it should be fairly obvious what I have done. I have tested it a bit but programs can always use more. Especialy those written at midnight as quickly as possible.
usage is program file1 file2 [skipfile] where skipfile contains a sorted list of line numbers (on seperate lines) to skip in both file1 and file2.
If this isn't what you wanted tell me why and what you do want.

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
	FILE *skipfile = NULL, *in1 = NULL, *in2 = NULL;
	int skip = -1;
	char line1[1024], line2[1024];
	int lineno = 0;
	int skipped = 1;

	if ((argc < 3) || (argc > 4))
	{
		fprintf(stderr, "usage: %s file1 file2 [skipfile]\nskipfile contains line numbers on seperate lines that will not be compared between file1 and file2 this file must be sorted\n", argv[0]);
		return 1;
	}
	if (argc == 4)
	{
		skipfile = fopen(argv[3], "r");
		if (!skipfile)
		{
			fprintf(stderr, "Could not open %s\n", argv[3]);
			return 2;
		}
	}

	in1 = fopen(argv[1], "r");
	in2 = fopen(argv[2], "r");
	if (!in1)
	{
		fprintf(stderr, "could not open %s\n", argv[1]);
		return 3;
	}
	if (!in2)
	{
		fprintf(stderr, "could not open %s\n", argv[2]);
		return 4;
	}

	while(1)
	{
		int eol = 0;

		lineno++;
		if (skipfile && skipped)
		{
			if (fgets(line1, 1024, skipfile) != NULL)
			{
				skip = atoi(line1);
				skipped = 0;
			}
		}
		while (!eol)
		{
			int i;
			char* res1 = fgets(line1, 1024, in1);
			char* res2 = fgets(line2, 1024, in2);
			if (!res1 || !res2)
			{
				if (feof(in1) || feof(in2))
				{
					if (feof(in1) && feof(in2))
					{
						fprintf(stderr, "Files are Identical\n");
						return 0;
					}
					else
					{
						fprintf(stderr, "Files differ in length\n");
						if (feof(in1))
							printf("%s is shorter\n", argv[1]);
						if (feof(in2))
							printf("%s is shorter\n", argv[2]);
						return -1;
					}
				}
				else
				{
					fprintf(stderr, "Error occured reading one of the files\n");
					return 5;
				}
			}
			
			
			for (i = 0; ; ++i)
			{
				if ((line1[i] == 0) || (line2[i] == 0))
				{
					if (skip == lineno)
					{
						break;
					}
					else if ((line1[i] == 0) && (line2[i] == 0))
						break;
					else
					{
						fprintf(stderr, "Difference on line %d\n", lineno);
						printf("%s\n%s\n", line1, line2);
						return -1;
					}
				}
				else if ((line1[i] == '\n') || (line2[i] == '\n'))
				{
					if (skip == lineno)
					{
						skipped = 1;
						eol = 1;
						break;
					}
					else if ((line1[i] == '\n') && (line2[i] == '\n'))
					{
						eol = 1;
						break;
					}
					else
					{
						fprintf(stderr, "Difference on line %d\n", lineno);
						printf("%s\n%s\n", line1, line2);
						return -1;
					}
				}
				else if (skip == lineno)
				{
					continue;
				}
				else if (line1[i] == line2[i])
				{
					continue;
				}
				else
				{
					fprintf(stderr, "Difference on line %d\n", lineno);
					printf("%s\n%s\n", line1, line2);
					return -1;
				}
			}
		}
		
	}
}
cjohnsto is offline